2015-09-04 46 views
1

上傳錯誤基本的異常實踐問題

這的截圖顯示基本ArithmeticException是給我的錯誤在NetBeans的例子。有任何想法嗎。

This is the screen shot of the errors

public class Exception { 

    public static void main (String[] args){ 

     try 
     {    
      double x = 0; 
      double y = 19000; 
      double z;  

      double practice() 
      { 
       double z = 4000; 
       return y - z; 
      } 

      public double practiceAgain() 
      { 
       double f = (9 + z); 
       return f/x;   
      } 
     } 
     catch (ArithmeticExecption t){ 
     System.out.println(t); 
     } 
    } 
+0

提供錯誤信息通常會有幫助... – TallTed

+0

沒有「catch」「finally」或資源聲明和「;」的「嘗試」預計雙重練習() – Brad

+0

請把完整的錯誤信息放到你的問題中。保持它們不同。你在上面的註釋中提供的內容表明你的代碼中缺少';'和/或錯位的'}'。 – TallTed

回答

0

作爲錯誤狀態:

不可編譯源代碼 - 在異常表達的非法啓動:主(Exception.java:13)

你的源由於多種原因代碼無效:

  1. 在一種方法中,不存在作爲範圍(即publicprotectedprivate) - 在您的錯誤消息:main方法中的第13行中提到。
  2. 裏面一個方法,你不能宣佈這樣的另一種方法(即上線18和24)

主要通過兩種方式來進行發佈代碼編譯:

上移的內容出來的main方法:

public class Exception { 
    double x = 0; 
    double y = 19000; 
    double z; 

    public static void main (String[] args) { 
     try { 
      practice(); 
      practiceAgain(); 
     } catch (ArithmeticExecption t){ 
      t.printStackTrace(); 
     } 
    } 

    double practice() { 
     z = 4000; 
     return y - z; 
    } 

    public double practiceAgain() { 
     double f = (9 + z); 
     return f/x;   
    } 
} 

還是在main方法直接做你的處理:

public class Exception { 
    public static void main (String[] args) { 
     try { 
      double x = 0; 
      double y = 19000; 
      double z = 4000; 
      double practice = y - z; 
      double f = (9 + z); 
      double practiceAgain = f/x; 
     } catch (ArithmeticExecption t){ 
      t.printStackTrace(); 
     } 
    } 
} 
+0

謝謝卡斯滕......感謝幫助......新的在這裏。 – Brad