exception

一、       Introduction

异常指不期而至的各种状况,如类找不到、类型不配、算术不合法等。异常是一个时间,发生在程序运行期间,干扰了正常的流程。Java异常都是对象,是Throwable子类的实例。Throwable的两个子类:Exception和Error。Error是程序无法处理的错误,表示运行应用程序中较严重的问题。例如java虚拟机运行错误,内存溢出等。Exception是程序本身可以处理的异常。在java应用程序中,异常处理机制为抛出异常和捕捉异常。

二、       捕捉异常

捕捉异常通过try-catch语句或者try-catch-finally语句实现。

例子:

public class ExceptionTest {

   public staticvoidmain(String[] args){

      Scanner in=new Scanner(System.in);

      System.out.println("请输入被除数:");

      try{

         int in1=in.nextInt();

         System.out.println("请输入除数:");

         int in2=in.nextInt();

         System.out.println(String.format("%d/%d=%d",in1,in2,in1/in2));

      }catch(InputMismatchException e){

         System.out.println("输入的值必须是整数");

         System.exit(1);

         //强行终止程序可以使用System.exit(1);

      }catch(ArithmeticException e){

         System.out.println("输入的除数不能为0");

      }catch(Exception e){

         System.out.println("错误");

      }finally{

         in.close();

         System.out.println("谢谢");

      }

   }

}

三、       抛出异常

自定义异常,进行抛出处理。一般步骤:

创建一个异常类,表示异常信息;异常类需要继承Exception类或者RuntimeException类;需要有参的构造函数;使用super关键字调用父类的有参构造函数。

规定正常人年龄范围在1-150之间的异常处理例子:

自定义异常类

public class AgeWrongException extends Exception {

   public AgeWrongException(String msg){

      super(msg);

   }

}

创建关于人类的年龄的类

public class Person {

   private intage;

   public voidsetAge(intage)throwsAgeWrongException{

      if(age<150&&age>0){

         this.age=age;

      }else{

         throw new AgeWrongException("年龄需要在0-150之间");

      }

   }

   public intgetAge(){

      return age;

   }

   public Person(intage)throwsAgeWrongException{

      setAge(age);

   }

}

测试

public class ThrowTest {

   public staticvoidmain(String[] args)throwsAgeWrongException{

      Person p=new Person(18);

      System.out.println(p.getAge());

      try{

         Person p2=new Person(-9);

         System.out.println(p.getAge());

      }catch(AgeWrongException e){

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

      }

   }

}

关于同时抛出多个异常类的例子:

创建多个自定义异常类,略

创建处理人的年龄的类

public class Person {

   private intage;

   public voidsetAge(intage)throwsAgeWrongException,AgeWrongException2,AgeWrongException3{

      if(age<150&&age>0){

         this.age=age;

      }else{

         throw new AgeWrongException("年龄需要在0-150之间");

      }

   }

   public intgetAge(){

      return age;

   }

   public Person(intage)throwsAgeWrongException, AgeWrongException2, AgeWrongException3{

      setAge(age);

   }

}

测试

public class ThrowTest {

   public staticvoidmain(String[] args)throwsAgeWrongException, AgeWrongException2, AgeWrongException3{

      Person p=new Person(18);

      System.out.println(p.getAge());

      try{

         Person p2=new Person(-9);

         System.out.println(p.getAge());

      }catch(AgeWrongException e){

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

      }

   }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值