@firstmiki 2017年1月12日12:03:32
一、异常的产生和捕获:
package Exception; /** * 演示了java中异常的产生和捕获 * @firstmiki */ import java.util.Scanner; public class Exception1 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); try{ String str = "123a"; int output = Integer.parseInt(str); //这一句就产生了异常 System.out.println(output); } catch(NumberFormatException e){ System.out.println(e.getMessage()); /*System.out.println(e.getClass()); System.out.println(e.getStackTrace());*/ e.printStackTrace(); } } }
运行结果:
For input string: "123a" java.lang.NumberFormatException: For input string: "123a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Exception.Exception1.main(Exception1.java:15)
二、异常语句:try...catch...finally...
注意下面代码中的16--19行:
package Exception; /* * finally语句 (重要) */ public class TestFinally { public static void testfinally(){ String str = "123a"; try{ int a = Integer.parseInt(str); System.out.println(a); }catch(Exception e){ e.printStackTrace(); System.out.println("exception"); return; //返回,不再执行下面语句; }finally{ //尽管加了return,奇怪的是,这里的finally语句被执行了; System.out.println("the end in finally"); } System.out.println("end"); } public static void main(String[] args) { testfinally(); } }
运行结果:
java.lang.NumberFormatException: For input string: "123a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Exception.TestFinally.testfinally(TestFinally.java:10) at Exception.TestFinally.main(TestFinally.java:25) exception the end in finally
三、throws关键字:
注意看下面的注释说明:
package Exception; /*throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常) 当某个方法可能会抛出某种异常时用于throws 声明可能抛出的异常,然后交给上层调用它的方法程序处理。*/ public class Testthrows { //throws:把异常向外面抛 public static void testthrows() throws NumberFormatException{ String str = "123a"; int a = Integer.parseInt(str); System.out.println(a); } public static void main(String[] args) { try{ testthrows(); }catch(Exception e){ System.out.println("我们在这里处理了一个异常"); e.printStackTrace(); } System.out.println("这里还是被执行了"); } }
运行结果:
我们在这里处理了一个异常 java.lang.NumberFormatException: For input string: "123a"这里还是被执行了 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Exception.Testthrows.testthrows(Testthrows.java:8) at Exception.Testthrows.main(Testthrows.java:14)
四、throw关键字:
package Exception; /*throw是语句抛出一个异常。语法:throw (异常对象); 如: throw e; 一般会用于程序出现某种逻辑时程序员主动抛出某种特定类型的异常。*/ public class Testthrow { public static void testthrow(int a) throws Exception{ if(a==1){ //直接抛出一个异常 throw new Exception("这里a有异常"); } System.out.println("这里a没有异常,a="+a); } public static void main(String[] args) { try { testthrow(1); } catch (Exception e) { e.printStackTrace(); } try { testthrow(0); }catch (Exception e) { e.printStackTrace(); } } }
运行结果:
java.lang.Exception: 这里a有异常 at Exception.Testthrow.testthrow(Testthrow.java:8) at Exception.Testthrow.main(Testthrow.java:14) 这里a没有异常,a=0