五种常见异常
1、NullPointerException 空指针
2、ClassNotFoundException 指定类不存在
3、ArithmeticException运算异常
4、ArrayIndexOutOfBoundsException数组下标越界
5、IllegalArgumentException方法的参数错误
6、IllegalAccessException 没有访问权限
小例子
import java.util.Scanner; public class Exception { int shang; public Exception(int chushu,int beichushu){ shang=chushu/beichushu; } public static void main(String[] args) { try { Scanner sc=new Scanner(System.in); String chushu=sc.nextLine(); String beichushu=sc.nextLine(); int a =Integer.parseInt(chushu); int b =Integer.parseInt(beichushu); Exception exception =new Exception(a, b); System.out.println(exception.shang); } catch (NumberFormatException e) { System.out.println("NumberFormatException"); e.printStackTrace(); // TODO: handle exception } catch (ArithmeticException e) { System.out.println("ArithmeticException"); // TODO: handle exception e.printStackTrace(); }finally { System.out.println("666"); } } }
时间: 2024-11-08 11:03:40