加载中...

Java 异常

1.Throwable的子类包含哪两个类?简述Java Error类与Exception类的区别

Error:致命异常,程序无法处理,如栈溢出,内存溢出(OutofMemoryError)等

Exception:非致命异常,程序可处理。分为checked(受检)异常和unchecked(非受检)异常。

2. Exception分为checked异常和unchecked异常,分别举例?

Exception分为checked与unchecked异常的根据是 编译器是否能够捕获这个异常。

引用: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition)

uncheck异常出现的原因在人,比如NullPointerException(空指针异常)

	public class Test {
    //    public static student s1;
        public static void main(String[] args) {
            student s1 = null;
            System.out.println(s1.name);
        }
    }
    
    class student{
        public String name;
        }
//输出:java.lang.NullPointerException

Java中没有指针,可以说是空引用异常,值得注意的是,若改为

    public class Test {
    //    public static student s1;
        public static void main(String[] args) {
            student s1 ;
            System.out.println(s1.name);
        }
    }

    class student{
        public String name;
        }
//输出: 可能尚未初始化变量s1
一个对象若未初始化,是不能够被使用的。

修改

    public class Test {
        public static student s1;
        
        public static void main(String[] args) {
            //student s1 ;
            System.out.println(s1.name);
        }
    }

    class student{
        public String name;
        }
//输出:java.lang.NullPointerException

//可以看出,编译器在为static对象s1分配空间后,s1空间里没有数据,输出s1.name出错

3.StackoverflowError和OutofMemoryError错误发生的情形和原因

????? StackoverflowError 是在超出给定内存时报错,如给定长度的数组越界使用时报错;

OutofMemoryError是在动态数组中,如ArrayList<>中超过最大范围报错;

public static void main(String[] args) {
            ArrayList<Long> b = new ArrayList<Long>();

            while(true){
                b.add(1231231L);
            }
        }
//逻辑:往ArrayList中一直加数
//跑了大概半分钟 输出 java.lang.OutOfMemoryError

4.异常处理的两种方式

  1. try...catch...finally
  2. throws声明抛出异常
//try...catch...finally
public static void main(String[] args) {
            int[] a = new int[100];

            try{
                System.out.println(a[101]);
            } catch(ArrayIndexOutOfBoundsException e){
                System.out.println("数组越界");
            }finally{
                System.out.println("继续");
            }
        }


//throws声明抛出异常

???
   

5. RuntimeException其中的异常

public class Test {
        public static void main(String[] args) throws ArrayIndexOutOfBoundsException {
            try{
                int t1 = 3/0;
            }catch(ArithmeticException e){
                System.out.println(e);
            }

            try{
                people p1 = null;
                System.out.println(p1.name);
            }catch(NullPointerException e){
                System.out.println(e);
            }

            /**
             * 类转化异常是父类的引用对象继承子类时,子类的强制转化报错
             */
            try{
                people p1 = new student("1");
                people p2 = new teacher("2");
                p2 = (teacher) p1;
            }catch (ClassCastException e){
                System.out.println(e);
            }

            try{
                int[] a = new int[10];
                System.out.println(a[11]);
            }catch(ArrayIndexOutOfBoundsException e){
                System.out.println(e);
            }
        }
    }

    class people
    {
        public String name;
        public  people(String name){
            this.name = name;
        }
    }

    class student extends people{
        public student(String name){
            super(name);
        }
    }

    class teacher extends people{
        public teacher(String name){
            super(name);
        }
    }

/**
output:
java.lang.ArithmeticException: / by zero
java.lang.NullPointerException
java.lang.ClassCastException: 测试包.student cannot be cast to 测试包.teacher
java.lang.ArrayIndexOutOfBoundsException: 11
**/

7 Throws 与 throw 的区别?

与 throws 不同的是,throw 语句用来直接拋出一个异常,后接一个可拋出的异常类对象

当 throw 语句执行时,它后面的语句将不执行,此时程序转向调用者程序,寻找与之相匹配的 catch 语句,执行相应的异常处理程序。如果没有找到相匹配的 catch 语句,则再转向上一层的调用程序。这样逐层向上,直到最外层的异常处理程序终止程序并打印出调用栈情况。

throw 关键字不会单独使用,它的使用完全符合异常的处理机制,但是,一般来讲用户都在避免异常的产生,所以不会手工抛出一个新的异常类的实例,而往往会抛出程序中已经产生的异常类的实例。


throws 方法不处理,某些异常

8 finally子句作用

不管try子块中的语句是否执行成功,finally都会执行

public static void main(String[] args) {
            try{
                int t1 = 3/0;
            }catch(ArithmeticException e){
                System.out.println(e);
            }finally{
                System.out.println("继续执行");
            }
        }
/*
output:
java.lang.ArithmeticException: / by zero
继续执行
*/
posted @ 2021-12-02 23:42  BlingDan  阅读(32)  评论(0)    收藏  举报