异常

什么是异常

如:写的某个模块,用户输入不符合要求;程序打开某个不存在的文件等。

异常:Exception(分为error与exception,error更严重些)

异常分两类:错误Error与异常Exception,Error无法预见,而Exception可预见

检查性异常、运行时异常(编译时未发现,运行后出现)、错误ERROR(栈溢出等)

Error主要有JVM与UI两类,Exception主要有IO异常与运行异常

如何捕获?为关键

Error类对象由Java虚拟机生成并抛出,大多数错误与代码编写者所执行的程序无关

JVM运行错误(Virtual MachineError):当JVM不再由继续执行操作所需的内存资源时,将会出现OutOfMemoryError,这些发生时Java虚拟机一般会选择线程终止。

JVM类定义错误(NoClassDefFoundError)、链接错误(LinkageError):这些错误不可查

Throwable > Error 及 Exception

Exception中的一个重要子类:RuntimeException:运行时异常,这类异常是不检查异常,可以选择捕获处理也可以不处理,是程序员个人问题。

其他的Exception为非运行时异常

Error是致命的错误,是程序无法控制与处理的,出现Error时JVM一般选择终止线程;

而Exception通常是可以处理的。
————————————————

异常处理机制

抛出异常

抓取异常

异常处理的五个关键字:try、catch、finally、throw、throws

try:尝试处理 catch:捕获 finally:无论执行与否前面的,都会执行 throw/throws:抛出

try、catch一般成对使用,finally无论前面是否执行,其本身都会被执行。
————————————————

package OOP.exception;

public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;
        System.out.println(a/b);
    }
}

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at OOP.exception.Test.main(Test.java:7)

    
    而:
    

public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;
        try{//代码块
            //try监控区域,结合catch
            System.out.println(a/b);//本身这一行会报错
        }catch (ArithmeticException e){//捕获异常,命名为e,e为此异常类的实例对象
            
            //做判断或输出
            System.out.println("程序出现异常,变量b不能为0");
        }
        finally {//程序无论是否出异常,都会执行的代码块
            System.out.println("finally");
        }


    }
}
输出:
程序出现异常,变量b不能为0
finally

try、catch是必须的,finally是非必须的,但finally是可以用来关闭程序的(善后)

e为此异常类的实例对象

处理异常

语法:

try{
}catch ( 想要捕获的异常类型 类型命名 ) {
}finally{
}

package OOP.exception;

public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;

        try{
            
            new Test().a();
        }catch (ArithmeticException e){/
          
            System.out.println("程序出现异常,变量b不能为0");//此时未能捕获到此异常类型
        }
        finally {//程序无论是否出异常,都会执行的代码块
            System.out.println("finally");
        }

    }
    public void a(){
        b();  //a、b互相调用,会令内存不足栈溢出
    }
    public void b(){
        a();
    }
}
输出:
    finally  //即:先输出finally中的再报错
Exception in thread "main" java.lang.StackOverflowError
    at OOP.exception.Test.b(Test.java:25)
    at OOP.exception.Test.a(Test.java:22)
    at OOP.exception.Test.b(Test.java:25)
    at OOP.exception.Test.a(Test.java:22)
    at OOP.exception.Test.b(Test.java:25)
    at OOP.exception.Test.a(Test.java:22)
//栈溢出



package OOP.exception;

public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;

        try{
           
            new Test().a();
        }catch (Error e){//捕获Error,命名为e
            
            System.out.println("程序出现Error");
        }
        finally {//程序无论是否出异常,都会执行的代码块
            System.out.println("finally");
        }

    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}
输出:
    程序出现Error
    finally    //即:此时通过catch捕获到了异常

只要是catch(级别高的异常类型),则能捕获到级别低的异常:

package OOP.exception;

public class Test {
    public static void main(String[] args) {
        int a =1;
        int b = 0;

        try{
            new Test().a();
        }catch (Throwable e){//e用于打印堆栈
            
            System.out.println("程序出现Error");
        }
        finally {
            System.out.println("finally");
        }


    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}
输出:
    程序出现Error
    finally    //即:此时通过catch捕获到了异常

catch:

catch ( 想要捕获的异常类型 命名 ){
}

还可层层递进捕获多个catch:(类似if-else)

此时应将级别低的异常放前面,不然一开始就直接捕获了所有异常(即从小到大捕获)

package OOP.exception;

public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        
        try{
            System.out.println(a/b);
        }catch (Error error){//Error与Exceptioon并列的级别
            System.out.println("Error");
        }catch (Exception exception){
            System.out.println("Exception");
        }catch (Throwable throwable){//级别最高的Throwable应放在最后的catch
            System.out.println("Throwable");
        }finally {
            System.out.println("finally");
        }
    
    }
}
输出:
    Exception
    finally

递进捕获多个异常时,若被某一层捕获,则后面的catch不会再继续执行(类似break),将会直接跳到最后的finally

IDEA自动生成try-catch:

package OOP.exception;

public class Test1 {
    public static void main(String[] args) {
        int a =1;
        int b =0;
        System.out.println(a/b);//选中需要try的语句,并按Ctrl+Alt+t
    }
}


自动变为:


public class Test1 {
    public static void main(String[] args) {
        int a =1;
        int b =0;
        try {
            System.out.println(a/b);
        } catch (Exception exception) {
            exception.printStackTrace();//此行为输出提示Exception类型,即:打印Error的的栈信息
            //可添加需要的语句块
            
        } finally {
            //自行添加
        }
    }
}



public class Test1 {
    public static void main(String[] args) {
        int a =1;
        int b =0;
        try {
            System.out.println(a/b);
        } catch (Throwable exception) {
            exception.printStackTrace();
            System.out.println("ssss");
            
            System.exit(0);//令程序执行到此后结束,后续都不再执行
        } finally {
            System.out.println("s");
        }
    }
}
输出:
java.lang.ArithmeticException: / by zero
    at OOP.exception.Test1.main(Test1.java:8)
ssss

System.exit(0); 令程序执行到此后结束,后续都不再执行

以上为try中的语句已知,即已大致知道哪些语句会发生异常

主动抛出异常:throw/throws

throw是主动制造异常,throws是抛出异常

package OOP.exception;

public class Test1 {
    public static void main(String[] args) {
        int a =1;
        int b =0;
        try {
            if(b==0){
                throw new ArithmeticException();//主动通过throw抛出异常
            }
            //System.out.println(a/b);
        } catch (Exception exception) {

            System.out.println("ssss");

        } finally {
            System.out.println("s");
        }
    }
}

在try中 throw new 指定异常类型();

便可在try中制造指定类型的异常,并可以通过后续catch捕获。在方法中使用较多(主动提示潜在的异常),如:

package OOP.exception;

public class Test1 {
    public static void main(String[] args) {
        new Test1().test(1,0);//调用方法,而方法本身只传递了参数,并未使用a/b,方法仅仅是存在异常风险
    }

    public void test(int a, int b){
        if(b==0){//通过if预先构想异常风险存在的情况,并在满足异常的预先出现条件后主动throw指定类型异常
            
            throw new ArithmeticException();//指定制造ArithmeticException异常类型
        }
        
    }
}

Exception in thread "main" java.lang.ArithmeticException
    at OOP.exception.Test1.test(Test1.java:10)
    at OOP.exception.Test1.main(Test1.java:5)



public class Test1 {
    public static void main(String[] args) {
        new Test1().test(1,0);
    }

    public void test(int a, int b){
        if(b==0){
            throw new ArrayIndexOutOfBoundsException();
        }
        //System.out.println(a/b);
    }
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at OOP.exception.Test1.test(Test1.java:10)
    at OOP.exception.Test1.main(Test1.java:5)

而假设方法中处理不了该异常,则应该在方法上抛出该异常:即throws

package OOP.exception;

public class Test1 {
    public static void main(String[] args) {
        try {
            new Test1().test(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();//catch中负责处理异常,可以防止程序因异常而导致的程序停止
        } finally {
        }
    }

    public void test(int a, int b) throws ArithmeticException{
        if(b==0){
            throw new ArithmeticException();
        }
        //System.out.println(a/b);
    }
}


无标签
打赏
评论区
头像