2021-05-04

面向对象处理异常

 

import java.util.Scanner;

public class first {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    /**int num1 = 100;//被除数
     System.out.println("请输入除数:");
     int num2 = scanner.nextInt();
     int num3 =num1/num2;//如果有异常那么后面所有的代码不执行
     System.out.println("商是:"+num3);
     System.out.println("这里是最后一行代码,end");
     /**
      * 异常的捕获
      * 三种语法:try {执行代码}catch(异常类   对象) {只能用来输出异常信息}finally {作用:无论是否报错,最终都会执
行}
      * try {}catch() {}
      * try {}finally {}
      */
     try {
         int num1 = 100;//被除数
     System.out.println("请输入除数:");
     int num2 = scanner.nextInt();
     int num3 =num1/num2;//如果有异常那么后面所有的代码不执行
     System.out.println("商是:"+num3);
         
     }catch(ArithmeticException a) {
         //0System.out.println("除数不能为零");
         /**
          * 抛出异常
          * throw
          * 1.抛出单个异常
          * 2.throw 错误对象
          * 3.throw 写在代码块里面

          */
         throw new ArithmeticException("除数不能为零");
     }finally {
     System.out.println("这里是最后一行代码,end");
     System.out.println("无论是否报错,都可以执行");
     }
}}

public class second {
public static void main(String[] args) {
    /**Scanner scanner = new Scanner(System.in);
    try {
    int num1 = 100;//被除数
     System.out.println("请输入除数:");
     int num2 = scanner.nextInt();
     int num3 =num1/num2;//如果有异常那么后面所有的代码不执行
     System.out.println("商是:"+num3);
     
     int []array = {1,2,3,4,5};
     System.out.println("请输入要查询的下标");
     int i = scanner.nextInt();
     int num = array[i];
     System.out.println(num);
    }catch(ArithmeticException a) {
        throw new ArithmeticException("除数不能为零");
    }catch(IndexOutOfBoundsException e){
        //throw new IndexOutOfBoundsException("下标越界");
    }*/
    second te = new second();
    try {
        te.exceptionTest();
    } catch (ArithmeticException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}
/**
 * throws 
 * 1.可以抛出多个异常
 *2.代码块外面
 * 3.throws 后面跟的是类名
 * 怎么用:在测试类中药使用throws的方法,要用try-catch块捕获异常

 */
public void exceptionTest() throws Exception,ArithmeticException {
    Scanner scanner = new Scanner(System.in);
    int num1 = 100;//被除数
     System.out.println("请输入除数:");
     int num2 = scanner.nextInt();
     int num3 =num1/num2;//如果有异常那么后面所有的代码不执行
     System.out.println("商是:"+num3);
     
     int []array = {1,2,3,4,5};
     System.out.println("请输入要查询的下标");
     int i = scanner.nextInt();
     int num = array[i];
     System.out.println(num);
}
}
自定义异常
public class Person {
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) throws MyException{
        if(age>0) {
            this.age= age;    
        }else {
            //抛出异常
            throw new MyException("年龄小于0,非法");
        }
        
    }
    public void etAge(int i) {
        // TODO Auto-generated method stub
        
    }
}
public class MyException extends Exception{
    //有参构造器
    public MyException(String massage) {
        super(massage);
    }
}
public class test {
public static void main(String[] args) throws MyException{
    Person p =new Person();
         p.etAge(-1);

        
 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值