bugstar

导航

18.异常.md

1.try...catch

2.异常了的继承机制

2.1基本概念

2.2常用异常

public static void main(String[] args) {
        //
        try {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            int c = a / b;
            System.out.println("输入的两个数相除结果为: " + c);
        } 
        //IndexOutOfBoundsException
        catch(IndexOutOfBoundsException ie){
            System.out.println("数组越界");
        }
        //NumberFormatException
        catch(NumberFormatException ne){
            System.out.println("数字格式异常");
        }
        //ArithmeticException
        catch(ArithmeticException ae){
            System.out.println("算术异常");
        }
        catch (Exception e) {
            System.out.println("未知异常");
        }      
    }

2.3多异常捕获

Java7后提供一个catch捕获多个异常

public static void main(String[] args) {
        //
        try {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            int c = a / b;
            System.out.println("输入的两个数相除结果为: " + c);
        } 
        //Multiple Exception
        catch(IndexOutOfBoundsException
            | NumberFormatException
            | ArithmeticException me){
            System.out.println("多个异常");
        }
        catch (Exception e) {
            System.out.println("未知异常");
        }      
    }

2.4获取异常信息

public static void main(String[] args) {
        //
        try {
            FileInputStream i = new FileInputStream("a.txt");
        } 
        //Multiple Exception
        catch(IOException ie){
            System.out.println("exception message: " + ie.getMessage());
            ie.printStackTrace();
        }
        catch (Exception e) {
            System.out.println("未知异常");
        }      
    }
exception message: a.txt (系统找不到指定的文件。)
java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.company.project.exception.ExceptionPrint.main(ExceptionPrint.java:11)

2.5finally回收资源

2.6Checked异常和Runtime异常

2.7throw抛出异常

package com.company.project.exception;
 
import java.io.FileInputStream;
import java.io.IOException;
 
public class ExceptionPrint {
 
    public static void main(String[] args)throws IOException {//这里抛出IOException
        //throw exception
        FileInputStream i = new FileInputStream("a.txt");
    }
     
    //定义的新函数抛出指定的异常
    public static void test() throws Exception {};
}


throw和throws区别:

  • throw用在方法内部,而throws用于方法的声明
  • throw使用需要new一个异常,throws是声明有什么类型的异常
  • throw后面只能有一个异常对象,throws后面一次可以多个异常

public class ExceptionPrint {
    /*throws & throw*/
    public static void main(String[] args)throws IOException {//这里抛出IOException
        FileInputStream i = new FileInputStream("a.txt");
    }
     
    //定义的新函数抛出指定的异常
    public static void test() throws Exception {};
    public void DoSomething(){
        int a = 1;
        int b = 2;
        if (a != b) {
            //可以定义自己的异常
            throwMyException(); 
            //也可以定义其他的异常,根据需要使用
            throw new IndexOutOfBoundsException();
        }
    }
     
    public static void throwMyException(){
        System.out.println("我自己定义的异常");
    }
}


2.8自定义异常类

package com.company.project.exception;
//MyNewException。java
public class MyNewException extends Exception {
    //无参构造
    public MyNewException(){};
    //有参构造
    public MyNewException(String msg){
        super(msg);
    }
     
}

2.9throw和catch一起使用

3.异常的作用

  • 捕获异常的作用是否就是输出一句话:友好的异常处理;收集定位错误情况帮助解决问题;异常情况加的应对提供帮助
  • 捕获异常是否就是不会Exception:不同的异常,需要处理的方式不一,特别是在实际项目中

4.finnaly

finnaly在有try语句块的时候才能使用,在jvm不退出的情况下一定会执行

posted on 2018-03-02 13:44  bugstar  阅读(140)  评论(0编辑  收藏  举报