【Java异常】处理异常

一:异常的处理:抓抛处理
过程一:"抛":程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象
        并将此对象抛出
        一旦抛出对象以后,其后的代码不再执行
过程二:"抓":可以理解为异常的处理方式:
                     ① try - catch - finally
                     ② throws
二:try-catch-finally的使用
  try{
       //可能出现异常的代码
  }catch(异常类型1 变量名1){
      //处理异常的方式1
  }catch(异常类型2 变量名2){
       //处理异常的方式2
  }
  ....
  finally{
      //一定会执行的代码
  }

举例:

public class ExceptionTest1 {
    @Test
    public void test1(){
        String str = "123";
        str = "abc";

        int num = 0;
        try{
            num = Integer.parseInt(str);
        }catch (NumberFormatException e){
            System.out.println("出现数值转化异常,不要着急..........");
        }catch (NullPointerException e){
            System.out.println("出现空指针异常了,不要着急..........");
        }catch (Exception e){
            System.out.println("出现异常了,不要着急.........");
        }
        System.out.println(num);

        System.out.println("hello----2");
    }
}

运行结果如下:

 

 还有两种常见的异常处理方式:

e.printStackTrace();

public class ExceptionTest1 {
    @Test
    public void test1(){
        String str = "123";
        str = "abc";

        int num = 0;
        try{
            num = Integer.parseInt(str);
        }catch (NumberFormatException e){
//          System.out.println("出现数值转化异常,不要着急..........");
            e.printStackTrace();
        }catch (NullPointerException e){
            System.out.println("出现空指针异常了,不要着急..........");
        }catch (Exception e){
            System.out.println("出现异常了,不要着急.........");
        }
        System.out.println(num);

        System.out.println("hello----2");
    }
}

运行结果:

e.getMessage() ----->返回一个字符串

 System.out.println(e.getMessage());

public class ExceptionTest1 {
    @Test
    public void test1(){
        String str = "123";
        str = "abc";

        int num = 0;
        try{
            num = Integer.parseInt(str);
        }catch (NumberFormatException e){
//          System.out.println("出现数值转化异常,不要着急..........");
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }catch (NullPointerException e){
            System.out.println("出现空指针异常了,不要着急..........");
        }catch (Exception e){
            System.out.println("出现异常了,不要着急.........");
        }
        System.out.println(num);

        System.out.println("hello----2");
    }
}

 运行结果:

 注意:catch中的异常类型声明的顺序

举例:将上文代码的catch的异常顺序交换

 

综上所述 :catch中的异常类型如果没有子父类关系,则谁声明在上,谁声明在下无所谓catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面,否则报错

=================================我是分割线=============================================
总结:
     1.finally是可选的
     2.使用try将可能出现异常代码包装起来,在执行过程中,一旦出现异常,就会生成一个异常类的对象根据此对象的类型,去catch中进行匹配
      3.一旦try中的异常对象匹配到某一个catch时,就会进入catch中进行异常处理一旦处理完成,就跳出当前的try—catch结构(在没有写finally的情况)继续执行其后的代码
      4.catch中的异常类型如果没有子父类关系,则谁声明在上,谁声明在下无所谓catch中的异常类型如果满足子父类关系,则要求子类一定声明在父类的上面,否则报错
      5.常用的异常对象处理的方式:
         ① String getMessage()
         ② printStackTrace()
      6.在try结构中声明的变量,再出了try结构以后,就不能别调用了
体会:使用try-catch-finally处理编译时异常,使得程序在编译就不在报错,但是运行时任可能报错
     先当与我们使用try-catch-finally将一个编译时可能出现异常,延迟到运行时出现

感谢观看!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值