1、动手动脑

源码

import javax.swing.*;
class AboutException {
   public static void main(String[] a)
   {
      int i=1, j=0, k;
      k=i/j;
 try
 {
  
  k = i/j;    // Causes division-by-zero exception
  //throw new Exception("Hello.Exception!");
 }
 
 catch ( ArithmeticException e)
 {
  System.out.println("被0除.  "+ e.getMessage());
 }
 
 catch (Exception e)
 {
  if (e instanceof ArithmeticException)
   System.out.println("被0除");
  else
  { 
   System.out.println(e.getMessage());
   
  }
 }
 
 finally
     {
       JOptionPane.showConfirmDialog(null,"OK");
     }

}

}
 
 
结果:
 
              

java把可能会发生错误的代码放进try语句块中,catch语句块中的代码用于处理错误,不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM会结束掉整个应用程序。

2、动手动脑

源码

public class CatchWho {
    public static void main(String[] args) {
        try {
             try {
                 throw new ArrayIndexOutOfBoundsException();
             }
             catch(ArrayIndexOutOfBoundsException e) {
                 System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");
             }
 
            throw new ArithmeticException();
        }
        catch(ArithmeticException e) {
            System.out.println("发生ArithmeticException");
        }
        catch(ArrayIndexOutOfBoundsException e) {
           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");
        }
    }
}

结果

ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException

源码

public class CatchWho2 {
    public static void main(String[] args) {
        try {
             try {
                 throw new ArrayIndexOutOfBoundsException();
             }
             catch(ArithmeticException e) {
                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
             }
            throw new ArithmeticException();
        }
        catch(ArithmeticException e) {
            System.out.println("发生ArithmeticException");
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
        }
    }
}

结果

ArrayIndexOutOfBoundsException/外层try-catch

3、动手动脑

源码

public class EmbededFinally {
   
 public static void main(String args[]) {
       
  int result;
       
  try {
           
   System.out.println("in Level 1");
          
    try {
               
    System.out.println("in Level 2");
  // result=100/0;  //Level 2
              
     try {
                  
      System.out.println("in Level 3");
                     
      result=100/0;  //Level 3
               
    }
               
    catch (Exception e) {
                   
     System.out.println("Level 3:" + e.getClass().toString());
               
    }
               
               
    finally {
                   
     System.out.println("In Level 3 finally");
               
    }
               
              
    // result=100/0;  //Level 2
           
    }
           
   catch (Exception e) {
              
     System.out.println("Level 2:" + e.getClass().toString());
          
    }
    finally {
               
    System.out.println("In Level 2 finally");
          
    }
            
   // result = 100 / 0;  //level 1
       
  }
       
  catch (Exception e) {
           
   System.out.println("Level 1:" + e.getClass().toString());
       
  }
       
  finally {
          
.    System.out.println("In Level 1 finally");
       
  }
   
 }
}
结果
in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

4、动手动脑

源码

public class SystemExitAndFinally {
   
 public static void main(String[] args)
    {
       
  try{
           
   System.out.println("in main");
           
   throw new Exception("Exception is thrown in main");
              //System.exit(0);
       
  }
       
  catch(Exception e)
         {
           
   System.out.println(e.getMessage());
           
   System.exit(0);
       
  }
       
  finally
       
  {
           
   System.out.println("in finally");
       
  }
   
 }
}
结果
in main
Exception is thrown in main
当存在try中有throw new Exception()时,finally不会执行
 
课堂测验
 
输入整数判断成绩等级,抛出异常
 
import java.util.Scanner;
class MyException extends Exception
{
 public MyException(String messege)
 {
  super(messege);
 }
}
class numbertest //自定义异常类
{
 public int score(int a) throws MyException  //当a<0或a>100时,抛出一个自定义异常
 {
  
  if(a<0 || a>100) 
  {
   throw new MyException("成绩输入有误");//抛出异常
  }  
  return a;//返回a
 }
}
public class test1{
 public static void main(String[] args) throws MyException
 {
  try
  {
   Scanner scan=new Scanner(System.in);
   System.out.println("请输入分数");
   int n = 0,i;
   n=scan.nextInt();
   numbertest k=new numbertest(); 
   try
   {
    int t=k.score(n);
    i=n/10;
    switch(i)
    {
    case 10:
    case 9:
     System.out.println("优");break;
    case 8:
     System.out.println("良");break;
    case 7:
     System.out.println("中");break;
    case 6:
     System.out.println("及格");break;
    default:
     System.out.println("不及格");break;
    }
   }
   catch(MyException e)
   {
    System.out.println(e);//输出
   }
   }
   catch(Exception e)//由于变量定义为int型,所以输入字符时,则输出该异常信息
   {
    System.out.println("输入格式不合法");//输出
   }
  }
}
结果

 

java异常处理课后作的更多相关文章

  1. java程序中的经常出现的的异常处理课后总结

    一.JDK中常见的异常情况 1.常见异常总结图 2.java中异常分类 Throwable类有两个直接子类: (1)Exception:出现的问题是可以被捕获的 (2)Error:系统错误,通常由JV ...

  2. Java 异常处理笔记

    Java程序运行过程中所发生的异常事件可分为两类: §错误(Error):JVM系统内部错误.资源耗尽等严重情况 §违例(Exception): 其它因编程错误或偶然的外在因素导致的一般性问题,例如: ...

  3. 谈谈你对Java异常处理机制的理解

    先谈谈我的理解:异常处理机制可以说是让我们编写的程序运行起来更加的健壮,无论是在程序调试.运行期间发生的异常情况的捕获,都提供的有效的补救动作,任何业务逻辑都会存在异常情况,这时只需要记录这些异常情况 ...

  4. 理解java异常处理机制

    1. 引子 try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解.不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单.听话. ...

  5. 2017.4.7 java异常处理总结

    目录 1.java异常处理的几种错误做法 2.异常处理示例 3.常用异常 4.异常类的继承关系 5.异常处理机制 6.Throw和Throws的区别 7.e.toString(), e.getCaus ...

  6. Java异常处理总结Exception\Error

    Java异常处理总结Exception\Error 2012-12-28 08:17:17|  分类: JAVA |  标签:java  |举报|字号 订阅   Java异常处理总结          ...

  7. 札记:Java异常处理

    异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...

  8. java异常处理(父子异常的处理)

    我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...

  9. Java 异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...

随机推荐

  1. Serverless Component 介绍和使用指南

    Serverless Component 是什么,我怎样使用它? Serverless Components 的目标是什么? 我们希望通过 Serverless Components 让广大开发者更加 ...

  2. #AcWing系列课程Level-2笔记——3. 整数二分算法

    整数二分算法 编写整数二分,记住下面的思路,代码也就游刃有余了! 1.首先找到数组的中间值,mid=(left+right)>>1,区间[left, right]被划分成[left, mi ...

  3. docker配置搭建gogs

    参考文献: https://www.yeboyzq.com/linux/ruanjiananzhuangweihu/1012.html https://www.jianshu.com/p/d92fd4 ...

  4. Linux 下安装 java

    yum 安装java 配置环境 /etc/profile export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7. ...

  5. kvm虚拟化安装及虚拟机创建

    1.开启虚拟化(VMware虚拟机为例),安装centos7操作系统. 2.查看CPU是否开启虚拟化 #zgrep  "vmx" /proc/cpuinfo 3.配置yum源,通过 ...

  6. 免费生成二维码接口,可直接嵌入到web项目中,附带嵌入方法,任意颜色二维码,任意大小二维码!

    在线体验连接:http://www.zhaimaojun.top/qrcode/ 你是否在项目中寻找方便而且免费的可以直接嵌入到项目中的二维码生成工具呢?你找到了这里,说明你已经找到了!不要犹豫直接拿 ...

  7. 【转】Servlet 九大对象和四个作用域

    隐式对象 说明 request 转译后对应HttpServletRequest/ServletRequest对象 response 转译后对应HttpServletRespons/ServletRes ...

  8. Constructing Roads POJ - 2421 最小生成树板子题

    #include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...

  9. 安装Elasticsearch到Linux(源码)

    运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:Elasticsearch-7.1.0 硬件要求:最低2核4GB 安装过程 1.源码安装JDK ...

  10. redis 安装 集群 主从 哨兵 docker

    安装redis 官方文档 docker run -d --net host -v /opt/myconfig/redis/redis.conf:/usr/local/etc/redis/redis.c ...