異常Exception
package exception;import java.io.FileInputStream;/** * 異常 : 程序出現了不正常的情況 * @author Angus * * 我們在編寫程序的時候,可能有很多問題存在,為了將來方便的表示浙西額的問題原因,類型,位置,java就提供了異常對象供我們使用 */public class ExceptionDemo { public static void main(String[] args) { int a = 10; int b = 0; System.out.PRintln(a / b); //Exception in thread "main" java.lang.ArithmeticException: / by zero // FileInputStream fis = new FileInputStream("a.txt"); }}類 Throwable
Throwable是java.lang包中一個專門用來處理異常的類。它有兩個子類,即Error和Exception,它們分別用來處理兩組異常類 Throwable * Error:嚴重問題, 需要修改代碼或者設備支持。。 * Exception:不嚴重 * 編譯期間:需要我們自己處理 * 運行期間:不需要處理,需要修改代碼或者傳遞參數package exception;import java.io.FileInputStream;import java.util.Scanner;/** * @author Angus * 出現怎么解決呢? * * 如果我們沒有處理,java虛擬機在會控制臺打印錯誤。。 * * A基本格式: * try{ * 可能發生問題的代碼 * }catch(異常類名 變量名){ * 異常處理 * } * B 拋出 * */public class ExceptionDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("請輸入數據"); int a = sc.nextInt(); int b = sc.nextInt(); try{ int c = a/b ; System.out.println(c); }catch(Exception e){ System.out.println(e); //異常輸出 } }}Throwable方法使用
package exception;import java.io.FileInputStream;import java.util.Scanner;/** * @author Angus * Throwable * * getMessage() 返回此 throwable 的詳細消息字符串。 * toString() 返回此 throwable 的簡短描述。 * printStackTrace() 將此 throwable 及其追蹤輸出至標準錯誤流 */public class ExceptionDemo { public static void main(String[] args) { int a = 10; int b = 0; try{ System.out.println(a /b ); }catch(Exception e){// System.out.println(e); System.out.println(e.getMessage()); // / by zero System.out.println(e.toString()); // java.lang.ArithmeticException: / by zero e.printStackTrace(); } }}finally簡單使用:package exception;/** * * @author Angus * 基本格式 * try{ * 有問題代碼 * }catch{ * 處理方案 * }finally{ * 永遠執行。。。 * } */public class FinallyDemo { public static void main(String[] args) { int a = 10; int b = 0; try{ System.out.println(a /b); }catch(Exception e){ e.printStackTrace(); }finally{ System.out.println("over"); } }}結果:測試:常見面試題
package exception;/** * * @author Angus * * 1;final,fianlly,finalize 區別? * 2:finally會永遠執行嗎? * 會,但是有一個特殊情況,在執行到finally之前JVM退出。。。 * 3:在catch中加入return finally代碼是否執行? * 會執行,在return之前執行 * 準確說在return之間。。。 */public class FinallyDemo { public static void main(String[] args) { // method1();// method2(); int result = method3(); System.out.println(result); } private static int method3() { int a = 10; int b = 0; try{ System.out.println(a /b); System.out.println("a1.."+a); }catch(Exception e){// e.printStackTrace(); a = 20; System.out.println("a2.."+a); return a; }finally{ System.out.println("over"); System.out.println("a3..."+a); a = 30; } return a; } private static void method2() { int a = 10; int b = 0; try{ System.out.println(a /b); }catch(Exception e){ e.printStackTrace(); return; //在catch中加入return finally代碼是否執行? }finally{ System.out.println("over"); } } private static void method1() { int a = 10; int b = 0; try{ System.out.println(a /b); }catch(Exception e){ e.printStackTrace(); System.exit(0); //finally會永遠執行嗎? }finally{ System.out.println("over"); } }}可以自己運行代碼查看結果。。。。拋出異常
package exception;import java.io.FileInputStream;import java.util.Scanner;/** * @author Angus * 拋出異常 * 格式; * 在方法名稱后跟關鍵字: throws 異常類名 * * 異常的分類: * A:Exception 編譯時異常 * B:RunTimeException 運行時異常 */public class ExceptionDemo { public static void main(String[] args) { try { method(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //告訴你這個方法可能有問題,需要注意。。但是沒有處理。。// private static void method() throws ArithmeticException{// int a = 10;// int b = 0;// System.out.println(a /b );// } private static void method() throws Exception{ int a = 10; int b = 0; System.out.println(a /b ); }}最后附上JDK使用文檔API 下載
新聞熱點
疑難解答