ClassNoFoundException 应⽤程序试图加载类,找不到对应的类
IllegalAccessException 拒绝访问⼀个类的时候
NoSuchFieldExcetion 请求的变量不存在
NoSuchMethodException ⽅法不存在
ArrayIndexOutOfBoundsException 数组索引越界
ClassCastException 强制失败抛出异常
NullPointerException 需要对象的地⽅使⽤ null 时,抛出该异常
NumberFormatException 将字符串转换成⼀种数值类型,但该字符串不能转换为适当格式时,抛出该异常
public synchronized Throwable getCause()
publicvoid printStackTrace()
try{ // 可能发⽣异常的代码 }catch(AExceptionName e){ //出异常的时候处理 }catch(BExceptionName e){ }fianall{ }
一个try代码块后面跟多个catch代码块的情况就叫多重捕获
try{ // 可能发⽣异常的代码 }catch(ExceptionName1 e1){ //出异常的时候处理 }catch(ExceptionName2 e2){ //出异常的时候处理 }
代码中发生异常,异常被抛给第一个catch块,如果不匹配则继续往下一个catch进行传递
try{ // 可能发⽣异常的代码 }catch(ExceptionName1 e1){ //出异常的时候处理 }finally{ //肯定执⾏的代码 }
或者
try{ // 可能发⽣异常的代码 }finally{ //肯定执⾏的代码 }
例子
public class Main { public static void readChar() throws IOException,RemoteException { int input = System.in.read(); } }
语法
throw new ExceptionName("异常信息");
throw new IOException("File not found");
当抛出异常被检查的异常,我们必须使用try-catch块来处理它,或者在方法声明中使用throws子句继续往外抛
public class BaseException extends Exception { private String errorMessage; private String errorCode; public BaseException(String errorCode, String errorMessage) { super(errorMessage); this.errorCode = errorCode; this.errorMessage = errorMessage; } public String getErrorMessage() { return errorMessage; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } }
原文链接:https://www.cnblogs.com/chenyanbin/p/13381120.html