2017-06-03 70 views
1

好吧...所以我正在學習java中的異常,我現在處於throw語句。我拋出Exception類的異常,然後再從catch塊重新拋出它以在主函數中處理它。但是,每當我把它作爲Exception類拋出時,我總是會在catch塊中得到一個Error(在我重新拋出它的時候會被main處理)。但是,只要我將引發和捕獲的異常更改爲某些特定的異常(如NullPointerException)有用!從異常類中拋出異常在java中

錯誤代碼:

class ThrowingExceptions { 
    static boolean enable3dRendering = false; 

    public static void main(String [] com) { 

     try { 
      renderWorld(); 
     } 
     catch(Exception e) { 
      System.out.println("Rendering in 2d."); 
     } 
    } 

    static void renderWorld() { 
     try{ 
      if(!enable3dRendering) { 
       System.out.println("3d rendering is disabled. Enable 3d mode to render."); 
       throw new Exception("3d mode Disabled."); 
      } 
      else { 
       System.out.println("The World is Empty!"); 
      } 
     } 
     catch(Exception e) { 
      System.out.println("Please handle the error"); 
      throw e; // It gives me an error here 
     } 
    } 
} 

工作代碼:

class ThrowingExceptions { 
    static boolean enable3dRendering = false; 

    public static void main(String [] com) { 

     try { 
      renderWorld(); 
     } 
     catch(NullPointerException e) { 
      System.out.println("Rendering in 2d."); 
     } 
    } 

    static void renderWorld() { 
     try{ 
      if(!enable3dRendering) { 
       System.out.println("3d rendering is disabled. Enable 3d mode to render."); 
       throw new NullPointerException("3d mode Disabled."); 
      } 
      else { 
       System.out.println("The World is Empty!"); 
      } 
     } 
     catch(NullPointerException e) { 
      System.out.println("Please handle the error"); 
      throw e; 
     } 
    } 
} 

爲什麼它不與Exception類的工作,並與它的子類的工作?

注: - 我在錯誤代碼得到的錯誤是未處理的異常類型異常

+0

當你在這裏提出一個問題,請隨時包括你所看到的到底是什麼錯誤。 – nhouser9

+0

在我看來,您需要閱讀有關已檢查和未經檢查的例外情況。 –

回答

1
  1. 運行時異常延長RuntimeException。他們不必被處理或宣佈。 它們可以由程序員或JVM引發。

  2. 檢查異常在其層次結構中有Exception,但不包含RuntimeException。他們 必須處理或宣佈。它們可以由程序員或JVM引發。

  3. Errors擴展Error類。它們由JVM拋出,不應該被處理或者聲明爲 。

當方法拋出檢查異常(1)時,您應該處理或重新發出它。

當方法拋出未經檢查的異常(2)時,您可以處理或重新拋出它,但它不是強制性的。

但每當我把它作爲Exception類,我總是在 錯誤catch塊(其中我重新把它扔在主處理)

這意味着,你的方法是拋出應該處理或重新拋出的檢查異常。

處理:

public class Main { 

    public static void main(String[] args) { 
     try { 
      throw new Exception(); 
     } catch (Exception e) { 
      try { 
       throw new Exception(); 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 
} 

重新拋出:

public class Main { 

    public static void main(String[] args) throws Exception { 
     try { 
      throw new Exception(); 
     } catch (Exception e) { 
      throw new Exception(); 
     } 
    } 
} 

你的情況:

// You declaring that the caller should handle exception 
static void renderWorld() throws Exception { 
     try { 
      if(!enable3dRendering) { 
       System.out.println("3d rendering is disabled. Enable 3d mode to render."); 
       throw new Exception("3d mode Disabled."); 
      } else { 
       System.out.println("The World is Empty!"); 
      } 
     } catch(Exception e) { 
      System.out.println("Please handle the error"); 
      // You cannot just throw uncheked exception here 
      // You should handle it yourself or a caller should do it 
      throw e; 
     } 
} 
0

更改

static void renderWorld() { ... } 

static void renderWorld() throws Exception { ... } 

應該解決這個問題。這是因爲運行時異常是未經檢查的異常。

建議您閱讀有關Checked和Unchecked例外的詳細信息 - Java: checked vs unchecked exception explanation

0

這是因爲它們是幾個Exception類繼承自類異常。每一個都可以扔,逮住但他們分爲兩組:

經過選中例外:

  1. 一個未經檢查的異常並不需要處理和 NullPointerException異常你嘗試是來自該組,所以你不需要在技術上關心它。
  2. A 檢查異常需要每次處理或者不會 編譯,這些異常類似IOException

由於基座異常對象可以選中和未選中以及編譯器關心它應處理每次。

如果你給它一個嘗試和改變NullPointerException異常IOException異常它不會編譯要麼導致它是一個的Checked Exception。所以它只是隨機的,你正好找到一種類型的異常你的代碼可以在沒有編譯錯誤的情況下工作。

欲瞭解更多信息請訪問我的博客文章吧: http://www.zoltanraffai.com/blog/?p=93