异常:
Thowable类有两个子类:运行时异常Exception和错误Error
异常的父类:Exception类
异常的常用关键字:throw、throws、 try catch 、finally
throw和throws是抛出异常,throw 是把新建的异常对象抛给调用者,而throws是把已经存在的异常抛给调用者去处理。
finally必须接在try catch 后面,try是用来捕获异常,catch是处理try捕获的异常,finally表示不管有没有执行try catch 语句,都要执行finally后面的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| try{ eq(s); } catch(Exception e){ String t=e.getMessage(); System.out.println("s不是ioexception"); e.printStackTrace(); } finally{ System.out.println("执行了finally语句"); } } public void eq(String s) throws Exception{ //throws已经存在的异常抛给调用者去处理 if(s.equals("ioexception")) System.out.println("s是"+s); else{ Exception e=new Exception(s); throw e; } }
|