まずエラーの原因から
Exception in thread "main" java.lang.ArithmeticException: / by zero
at MyApp.div(MyApp.java:6)
at MyApp.main(MyApp.java:10)
java
1 2 3class MyException extends Exception { 4 public MyException(String s) { 5 super(s); 6 } 7} 8 9public class MyApp { 10 11 public static void div(int a, int b) { 12 try { 13 if (b < 0) { 14 throw new MyException("not minus!") 15 } 16 System.out.println(a / b); 17 } catch (ArithmeticException e) { 18 System.err.println(e.getMessage()); 19 } catch (MyException e) { 20 System.err.println(e.getMessage()); 21 } finally { 22 System.out.println("-- end --"); 23 24 } 25 26} 27 28 public static void main(String[] args) { 29 div(3, 0); 30 div(5, -2); 31 32 } 33 34 35}
インスタンス(変数)を投げていてメソッドではないのになぜ()がいるのか
java
1throw new MyException("not minus!")
string intなどのように小文字のイメージが強く、クラスの一文字は大文字で定数は全文字大文字だと習いましたが今回はなぜ一文字目が大文字でしょうか?
それとこのeはいったいなにものでしょうか?変数でしょうか?ほかに見当たらなく、いきなり出てきたのでerrorのeだと思いますが、定義されているのでしょうか?
型を指定するのにはじめは大文字になることはわかりました。よってeは変数である。
java
1ArithmeticException e 2e.getMessage
よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/02 12:19