Exception异常

1.运行异常 

public class Wang {
    public static void main(String[] args) {
        //ArithmeticException异常:试图除以0
        int a=0;
        int b=1;
        if(a!=0){
            System.out.println(b/a);
        }


        //NullPointerException异常:空指针
        String str=null;
        if(str!=null){
            System.out.println(str.length());
        }

        
        //ArrayIndexOutOfBoundsException异常:数组越界
        int[] arr=new int[5];
        int a2=5;
        if(a2<arr.length){
            System.out.println(arr[a2]);
        }

        //强制转型异常
        //...
    }
}

2.CheckedException已检查异常

2.1使用try-catch处理异常

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Wang {
    public static void main(String[] args) {
        FileReader reader=null;
        try {
             reader =new FileReader("e:/a.txt");

            char c1=(char)reader.read();
            System.out.println(c1);
        } catch (FileNotFoundException e) {//子类异常在父类异常前面
            System.out.println("step2");
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            System.out.println("step3");
            try{
                if(reader!=null){
                    reader.close();
                }

            }catch(IOException e){
                e.printStackTrace();
            }



        }

    }
}

2.2 使用throws声明异常

import java.io.FileReader;
import java.io.IOException;

public class Wang {
    public static void main(String[] args) throws IOException {
        readMyFile();
        }
        public static void readMyFile() throws IOException {
            FileReader reader=null;
            reader =new FileReader("e:/a.txt");
            char c1=(char)reader.read();
            System.out.println(c1);
            if(reader!=null){
                reader.close();
            }
        }
   
    }

3.自定义异常

public class Wang {
    public static void main(String[] args) {
        Person p=new Person();
        p.setAge(-10);
    }

}
class Person{
    private int age;
    public int getAge(){
        return age;
    }
    public void  setAge(int age){
        if(age<0){
            throw new IllegalAgeException("年龄不能为负数");
        }
        this.age=age;
    }
}


class IllegalAgeException  extends RuntimeException{
    public IllegalAgeException(){

    }
    public IllegalAgeException(String msg){
        super(msg);
    }

}
public class Wang {
    public static void main(String[] args) {
        Person p=new Person();
        p.setAge(-10);
    }

}
class Person{
    private int age;
    public int getAge(){
        return age;
    }
    public void  setAge(int age){
        if(age<0){
            try {
                throw new IllegalAgeException("年龄不能为负数");
            } catch (IllegalAgeException e) {
                e.printStackTrace();
            }
        }
        this.age=age;
    }
}


class IllegalAgeException  extends Exception{
    public IllegalAgeException(){

    }
    public IllegalAgeException(String msg){
        super(msg);
    }

}

 

Exception in thread "main" cn.sxt.array2.IllegalAgeException: 年龄不能为负数
    at cn.sxt.array2.Person.setAge(Wang.java:21)
    at cn.sxt.array2.Wang.main(Wang.java:10)
 

 总结:

1.避免使用异常处理代替错误处理,会降低程序的清晰性,效率低下

2.处理异常不可代替简单测试---只有在异常情况下使用异常处理

3.不要进行小粒度的异常处理---应该将整个任务包装在一个try语句块中

4.异常往往在高层处理

       

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值