异常的分类
【1】异常分层结构
注意:程序中语法错误,逻辑错误都不属于上面的Error,Exception
【2】运行时异常
public class Test04 { //这是一个main方法:是程序的入口 public static void main(String[] args) { //运行时异常; int[] arr={1,2,3}; System.out.println(arr.length); //int[] arr2=null; //System.out.println(arr2.length); System.out.println(arr[10]); } }
【3】检查时异常
处理方式1:try-catch嵌套
package com.msd.test01; /** * 开发人:liu * 日期:10:49:37 * 描述:IntelliJ IDEA * 版本:1.0 */ public class Test05 { //这是一个main方法:是程序的入口 public static void main(String[] args) { //检查异常 try { try { Class.forName("com.msb.test").newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
处理方式2:多重catch
public class Test05 { //这是一个main方法:是程序的入口 public static void main(String[] args) { //检查异常 try { Class.forName("com.msb.test").newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } } }
处理方式3:throws
public class Test05 { //这是一个main方法:是程序的入口 public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { //检查异常 Class.forName("com.msb.test").newInstance(); } }