Java异常处理

一、捕获异常

异常是导致程序中断运行的一种指令流,如果不对异常进行正确处理,则可能导致程序的中断执行,造成不必要的损失。

1、异常范例

【示例-1】空指针异常

代码

class Exc{
    int i = 10;
}
public class Test49 {
    public static void main(String[] args) {
        Exc c = new Exc();
        System.out.println(c.i);
        Exc b = null;
        System.out.println(b.i); //此部分会报空指针异常
    }
}
结果:
10
Exception in thread "main" java.lang.NullPointerException
    at cn.sec.ch01.Test49.main(Test49.java:16)

【示例-2】算数异常

代码

public class Test49 {
  public static void main(String[] args) {
      int a = 10;
      int b = 0;
      int temp = a/b;
      System.out.println(temp);
  }
}
异常结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
  at cn.sec.ch01.Test49.main(Test49.java:16)

2、处理异常

异常格式:
try{
异常语句
}catch(Exception e){
}finally{
一定会执行的代码
}
示例
代码

public class Test49 {
    public static void main(String[] args) {
        
        int a = 10;
        int b = 0;
        int temp = 0;
        try {
            temp = a/b;
        } catch (Exception e) {
            System.out.println(e);
        }
        
        System.out.println(temp);
    }
}
结果:
java.lang.ArithmeticException: / by zero
0

二、常见异常

  • 数组越界异常:ArrayIndexOutOfBoundsException
  • 数字格式化异常:NumberFormatException
  • 算数异常:ArithmeticException
  • 空指针异常:NullPointerException
代码一
class Exam{
    int a = 10;
    int b = 0;  
}
public class Test50 {

    public static void main(String[] args) {
        Exam e = null; //创建对象
        e = new Exam();//实例化
        
        int temp = 0;
        try {
            temp = e.a/e.b;
            System.out.println(temp);
        } catch (NullPointerException e2) {
            System.out.println("空指针异常:"+e2);
        } catch (ArithmeticException e2) {
            System.out.println("算数异常:"+e2);
        }finally {
            System.out.println("程序退出");
        }
    }
}
结果:
算数异常:java.lang.ArithmeticException: / by zero
程序退出

代码二
class Exam{
    int a = 10;
    int b = 10;
}
public class Test50 {

    public static void main(String[] args) {
        Exam e = null; //创建对象
//      e = new Exam();//实例化
        
        int temp = 0;
        try {
            temp = e.a/e.b;
            System.out.println(temp);
        } catch (NullPointerException e2) {
            System.out.println("空指针异常:"+e2);
        } catch (ArithmeticException e2) {
            System.out.println("算数异常:"+e2);
        }finally {
            System.out.println("程序退出");
        }
    }
}
结果:
空指针异常:java.lang.NullPointerException
程序退出

代码三
class Exam{
    int a = 10;
    int b = 10;
}
public class Test50 {

    public static void main(String[] args) {
        Exam e = null; //创建对象
        e = new Exam();//实例化
        
        int temp = 0;
        try {
            temp = e.a/e.b;
            System.out.println(temp);
        } catch (NullPointerException e2) {
            System.out.println("空指针异常:"+e2);
        } catch (ArithmeticException e2) {
            System.out.println("算数异常:"+e2);
        }finally {
            System.out.println("程序退出");
        }
    }
}
结果:
1
程序退出

三、throws关键字

  • 在定义一个方法的时候可以使用throws关键字声明,使用throws声明的方法表示此方法不处理异常,抛给方法的调用者处理。如果是主方法抛出异常,就是JVM进行处理
  • 格式:public void tell() throws Exception{}
示例
代码

public class Test51 {
    public static void main(String[] args) {
        try {
            tell(10, 0);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    public static void tell(int i,int j) throws ArithmeticException{
        int temp = 0;
        temp = i/j;
        System.out.println(temp);
    }
}
结果:
java.lang.ArithmeticException: / by zero

四、throw关键字

throw关键字抛出一个异常,抛出的时候直接抛出异常类的实例化对象即可。

示例
代码

public class Test52 {
    public static void main(String[] args) {
        try {
            throw new Exception("实例化异常对象");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
结果:
java.lang.Exception: 实例化异常对象

五、自定义异常

自定义异常直接集成Exception就可以完成自定义异常类。

示例
代码

package cn.sec.ch01;

class MyException extends Exception{
    public MyException (String msg) {
        super(msg);
    }
}
public class Test53 {
    public static void main(String[] args) {
        try {
            throw new MyException("自定义异常");
        } catch (MyException e) {
            System.out.println(e);
        }
    }
}
结果:
cn.sec.ch01.MyException: 自定义异常

你可能感兴趣的:(Java异常处理)