异常的捕获及处理

异常的产生

当遇到异常,执行到异常语句时,程序会中断执行,后面的也不会执行,
出现异常。

public class Love{
    public static void main(String []args){
        System.out.println("1");
        System.out.println("2"+(10/0));
        System.out.println("3");
    }
}
----------------------------------------------------------
F:\test>java Love
1
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Love.main(Love.java:4)

处理异常(try...catch)

组合方式:try...catch、try...catch...finally、try...finally


格式
public class Love{
    public static void main(String []args){
        System.out.println("1");
        try{
            System.out.println("2"+(10/0));
            }catch(ArithmeticException e){
                System.out.println("Error");
            }
        System.out.println("3");
    }
}
--------------------------------------------------
F:\test>java Love
1
Error
3

输出异常信息(使用printStackTrace();)

public class Love{
    public static void main(String []args){
        System.out.println("1");
        try{
            System.out.println("2"+(10/0));
            }catch(ArithmeticException e){
                e.printStackTrace();
            }
        System.out.println("3");
    }
}
--------------------------------------------------------------
F:\test>java Love
1
java.lang.ArithmeticException: / by zero
        at Love.main(Love.java:5)
3

处理异常(try...catch...finally)

不管出不出异常,都会执行

public class Love{
    public static void main(String []args){
        System.out.println("1");
        try{
            System.out.println("2"+(10/0));
            }catch(ArithmeticException e){
                e.printStackTrace();
            }finally{
                System.out.println("usually");
            }
        System.out.println("3");
    }
}
-------------------------
F:\test>java Love
1
java.lang.ArithmeticException: / by zero
        at Love.main(Love.java:5)
usually
3
--------------------------------------------------
public class Love{
    public static void main(String []args){
        System.out.println("1");
        try{
            System.out.println("2"+(10/1));
            System.out.println("usually");
            }catch(ArithmeticException e){
                e.printStackTrace();
            }finally{
                System.out.println("usually");
            }
        System.out.println("3");
    }
}
-----------------------------------
F:\test>java Love
1
210
usually
usually
3

一、出现异常 用户什么都不输入(F:\test>java Love)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at Love.main(Love.java:5)

二、用户输入的参数不是数字(F:\test>java Love a b)

Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at Love.main(Love.java:5)

三、被除数为0(F:\test>java Love 10 0)已处理

F:\test>java Love 10 0
1
java.lang.ArithmeticException: / by zero
        at Love.main(Love.java:7)
usually
3

统一处理

public class Love{
    public static void main(String []args){
        System.out.println("1");
        try{
            int x = Integer.parseInt(args[0]);
            int y = Integer.parseInt(args[1]);
            System.out.println("2"+(x/y));
            System.out.println("usually");
            }catch(ArithmeticException e){
                e.printStackTrace();
            }catch(ArrayIndexOutOfBoundsException e){
                e.printStackTrace();
            }catch(NumberFormatException e){
                e.printStackTrace();
            }finally{
                System.out.println("usually");
            }
        System.out.println("3");
    }
}

异常的处理流程

下面是两个异常类的继承结构


NumberFormatException

ArithmeticException

可以发现所有的异常都是Throwable的子类,而Throwable下有两个子类。


Throwable下的两个子类

Error和Exception的区别
Error:JVM错误,此时的程序还没有执行,如果没有执行用户,则无法处理;
Exception:程序运行过程中产生的异常,用户可以处理。

Java异常处理流程


Java异常处理流程

使用Exception处理异常

Exception包含了子类异常,可以全处理

public class Love{
    public static void main(String []args){
        System.out.println("1");
        try{
            int x = Integer.parseInt(args[0]);
            int y = Integer.parseInt(args[1]);
            System.out.println("2"+(x/y));
            System.out.println("usually");
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                System.out.println("usually");
            }
        System.out.println("3");
    }
}

throws关键字

用throws抛出异常后,不管有没有异常,都要用try处理;不要在主方法上加throws Exception,可以加。

class MyMath{
    public static int div(int x,int y)throws Exception{
        return x/y;
    }
}
public class Love{
    public static void main(String []args){
        try{
            System.out.println(MyMath.div(10,2));
            }catch(Exception e){
                e.printStackTrace();
            }
    }
}
------------------------------------------------------------------
class MyMath{
    public static int div(int x,int y)throws Exception{
        return x/y;
    }
}
public class Love{
    public static void main(String []args)throws Exception{
            System.out.println(MyMath.div(10,2));
    }
}

throw(手工抛异常)

public class Love{
    public static void main(String []args){
        try{
            throw new Exception("Error");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

throw和throws区别


throw和throws区别

异常处理标准格式

正常代码

class MyMath{
    public static int div(int x,int y){
        int result = 0;
        System.out.println("Start");
        result =  x/y;
        System.out.println("End");
        return result;
    }
}
public class Do{
    public static void main(String []args){
        System.out.println(MyMath.div(10,2));
    }
}

产生异常(出现异常后,后面的语句都不会执行,而是顺着throws抛给了主方法)

package test;

class MyMath{
    public static int div(int x,int y)throws Exception{
        int result = 0;
        System.out.println("Start");
        result =  x/y;
        System.out.println("End");
        return result;
    }
}
public class Do{
    public static void main(String []args){
        try{
        System.out.println(MyMath.div(10,0));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
----------------------------
Start
java.lang.ArithmeticException: / by zero
    at test.MyMath.div(Do.java:7)
    at test.Do.main(Do.java:15)

处理异常(异常出现后,继续抛)

//标准
package test;

class MyMath{
    public static int div(int x,int y)throws Exception{
        int result = 0;
        System.out.println("Start");
        try{
        result =  x/y;
        }catch(Exception e){
            throw e;
        }finally{
        System.out.println("End");
        }
        return result;
    }
}
public class Do{
    public static void main(String []args){
        try{
        System.out.println(MyMath.div(10,0));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
------------------------------------------------------------------
//交给主方法处理
package test;

class MyMath{
    public static int div(int x,int y)throws Exception{
        int result = 0;
        System.out.println("Start");
        try{
        result =  x/y;
        }finally{
        System.out.println("End");
        }
        return result;
    }
}
public class Do{
    public static void main(String []args){
        try{
        System.out.println(MyMath.div(10,0));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

Runtime类

观察代码

public class Do{
    public static void main(String []args){
        int temp = Integer.parseInt("100");
        System.out.println(temp);
    }
}

观察parseInt方法



NumberFormatException继承结构


NumberFormatException

Exception和RuntimeException的区别
Exception和RuntimeException的区别

Exception和RuntimeException的区别

assert(关键字)


自定义异常(异常也可以继承)

自定义异常
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 导语 学完异常的捕获及处理就懂的情书。 主要内容 异常的产生以及对于程序的影响 异常处理的格式 异常的处理流程(核...
    一个有故事的程序员阅读 4,371评论 4 4
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • Java异常类型 所有异常类型都是Throwable的子类,Throwable把异常分成两个不同分支的子类Erro...
    予别她阅读 956评论 0 2
  • 这是一篇只有四百多字的短文,由德裔美籍人塞缪尔.厄尔曼所作,至今已有70余年,魅力依然不减,其优美的语句成为很多英...
    牛奶tiger阅读 503评论 0 0
  • 很久都没时间来整理自己的文章了,那是因为我偷偷去做了一件小事———打探天猫商家内部券的消息去了,今天给大家...
    花开只为蝶恋花阅读 767评论 0 0