不對。java
異常分兩類,runtime異常和非runtime異常。數組
class SelfGenerateException extends Exception { SelfGenerateException(String msg) { super(msg); //調用Exception的構造方法 } static void throwOne() throws SelfGenerateException { int a = 1; if (a == 1) { //若是a爲1就認爲在特定應用下存在異常,改變執行路徑,拋出異常 throw new SelfGenerateException("a爲1"); } } public static void main(String args[]) { try { throwOne(); } catch (SelfGenerateException e) { e.printStackTrace(); } } }
結果:設計
SelfGenerateException: a爲1 at SelfGenerateException.throwOne(test.java:10) at SelfGenerateException.main(test.java:16)
JDK解釋以下:指針
當應用程序試圖在須要對象的地方使用 null 時,拋出該異常。這種狀況包括:
調用 null 對象的實例方法。
訪問或修改 null 對象的字段。
將 null 做爲一個數組,得到其長度。
將 null 做爲一個數組,訪問或修改其時間片。
將 null 做爲 Throwable 值拋出。
應用程序應該拋出該類的實例來指示其餘對 null 對象的非法使用。code
public class test { public static void aMethod() throws Exception{ try{ throw new Exception(); } //------------------黑體------------------------------------------------ catch(Exception e){ System.out.println("exception000"); } //---------------------------------------------------------------------- //--------------------斜體----------------------------------------------- finally{ System.out.println("exception111"); } //--------------------------------------------------------------------------- } public static void main(String[] args){ try{ aMethod(); } catch(Exception e){ System.out.println("exception"); } System.out.println("finished"); } }
輸出:
exception000
exception111
finished對象
去黑體輸出:
exception111
exception
finished資源
若是再將斜體代碼去掉:it
Error:(4, 9) java: 'try' 不帶有 'catch', 'finally' 或資源聲明io
就是說try不能單獨存在而不帶有catch或者finally聲明編譯
再去掉aMthod裏的try後
輸出:
exception
finished
public class test{ public static String output =""; public static void foo(int i){ try{ if(i==1) {throw new Exception();} output += "1"; } catch(Exception e){ output += "2"; return; } finally{output += "3";} output += "4"; } public static void main(String args[]){ foo(0); foo(1); System.out.println(test.output); } }
結果:13423
解釋:134是foo(0)輸出的,23是foo(1)輸出的。foo(1)catch裏return後,也會執行finally,可是finally以後的東西就不在執行了。foo(0)不會執行return語句,因此能夠執行finally後面的語句
public class test { public static void main(String[] args) { try { String s = null; System.out.println(s.charAt(0));//空指針異常 //System.out.println(1/0);//除零異常 } catch (NullPointerException e) { System.out.println("空指針異常"); } catch (ArithmeticException e) { System.out.println("計算異常"); } catch (Exception e) { System.out.println("其餘異常"); e.printStackTrace(); } } }
import java.util.Arrays; import java.util.Scanner; public class test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[10]; int index = 0; while (true) { try { arr[index] = sc.nextInt(); if(index!=9) index++; else{ break; } } catch (Exception e) { sc.next(); System.out.println("輸入類型有誤,請從新輸入"); } } Arrays.sort(arr); System.out.println("max=" + arr[index]); System.out.println("min=" + arr[0]); } }
public void method()throws TimedOutException { success= connect(); if(success == -1) { throw new TimedOutException(); } }