java 中的异常处理:
java 中的异常处理有三种
throw throws try{ 代码块}catch(Exception e){处理方案}
那我们就来讲一下这三个的异同吧。
1,throw 是“抛出”的意思,在程序中,我们如果需要把一个异常抛出,就在改代码后面加一个 throw
用法:
throw 是需要new 出来的 ,下面我们来做个实例
我要抛出一个 异常 ,
throw new Exception (“这是我抛出的一个异常”);
当运行到这句代码的时候,程序就会把这个异常抛出,不会再往下运行了
注,当创建throw时,会提示 在方法()后加 throws ,这是因为要申明一下这个方法需要或者可能会抛出异常。
2.throws 申明一个异常,这个很好理解,他的用法就是在每个方法的()后面,它可以定义多个异常类型
例如 Exception IoException 等
例如:
System.out.println(" _oo0oo_ ");
System.out.println(" o8888888o ");
System.out.println(" 88\" . \"88 ");
System.out.println(" (| - - |) ");
System.out.println(" 0\\ 3 /0 ");
System.out.println(" ___/`---'\\___ ");
System.out.println(" .' \\\\| |// '. ");
System.out.println(" / \\\\||| : |||// \\ ");
System.out.println(" / -||||| -:- |||||- \\ ");
System.out.println(" | | \\\\\\ - /// | | ");
System.out.println(" | \\_| ''\\---/'' | _/| ");
System.out.println(" \\ .-\\__ '_' __/ -. / ");
System.out.println(" ____'. .' /--.--\\ . .' ");
System.out.println(" .\"\" '< `.____\\_<|>_/____.' >' \"\". ");
System.out.println(" | | : `- \\`.;`\\ _ /`;.`/ - ` : | | ");
System.out.println(" \\ \\ `-. \\_ __\\ /__ _/ .-` / / ");
System.out.println(" =====`-.____`-.___ \\____—____/ ___.-`____.-`===== ");
System.out.println();
}
- try{ 代码块}catch(Exception e){处理方案},这个比较常用,他的作用和前面两者刚好相反,前面是抛出异常,而他是捕获异常, 它 由try catch finally 三部分组成,
try{} 是用来写需要运行的逻辑代码,
catch(异常类型){
//处理方案
}
catch(异常类型){
//处理方案
}
catch(异常类型){
//处理方案
}
finally{
//必须执行的代码块
}
异常的类型就在catch中申明,只能同时申明一个异常,如果你出现的异常有很多种,那可以选择 多写几个catch,在catch中处理对应的异常。注,如果上一个catch中 申明的异常级数大于下一个catch中申明的级数,则编译不通过。而且catch只能成功运行一个异常类型中的代码块
例如:
try {
String m = "1111111111111";
int i = 0;
i = Integer.parseInt(m);
System.out.println(i);
} catch (NumberFormatException e) {
System.out.println("超出了int类型的边界值");
e.printStackTrace();
}catch (Exception e){
System.out.println("22222222222222222222222222222");
e.printStackTrace();
}finally {
System.out.println("结束");
}
}
java.lang.NumberFormatException: For input string: "1111111111111"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at java.lang.Integer.parseInt(Integer.java:615)
at Test.main(Test.java:8)
超出了int类型的边界值
结束
这是一个典型的类型转换的异常,int 的边界值是11位,如果大于这个值时,jvm就会抛出一个java.lang.NumberFormatException: For input string: "1111111111111"的异常。
e.printStackTrace(); 这个句代码可以让你更快速的定位问题的所在地方,
下面是这个方法的源码
private void printStackTrace(PrintStreamOrWriter s) {
// Guard against malicious overrides of Throwable.equals by
// using a Set with identity equality semantics.
Set<Throwable> dejaVu =
Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
dejaVu.add(this);
synchronized (s.lock()) {
// Print our stack trace
s.println(this);
StackTraceElement[] trace = getOurStackTrace();
for (StackTraceElement traceElement : trace)
s.println("\tat " + traceElement);
// Print suppressed exceptions, if any
for (Throwable se : getSuppressed())
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
// Print cause, if any
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
}
}
他会打印出一个异常的类型 和 出现异常的位置 和原因
总结:
1 throw 和 throws 都是抛出异常 ,不同的是throw 是抛出一个异常,而throws是申明一个异常的类型,遇到异常后不会执行下面的代码。
2.try{}catch(){} finally{} 捕获异常,出现异常后会执行catch代码块中的代码 ,能写多个异常类型,可在每个异常类型中,处理不同的异常情况,非常简明。
在不同场景中灵活运用才是王道。