2016-11-09 8 views
0

質問には、ZeroEnteredExceptionNegativeValueExceptionという2つの例外を定義してください。どちらも例外をスローする原因となった値を示す変数privateを持っています。また、両方のために適切なコンストラクタを定義します。 "ここにコンストラクタを配置するにはどうすればいいですか?"発生する可能性がある他の例外はどのようにコード化できますか?

このプログラムでは、発生する他の例外も処理する必要があります。必要に応じて適切なキャッチブロックを使用してください。 "何が起こる可能性がありますか?"

import java.util.Scanner; 

class ZeroEnteredException extends Exception { 
    public ZeroEnteredException(String s) { 
     super(s); 
    } 
} 

class NegativeValueException extends Exception { 
    public NegativeValueException(String s) { 
     super(s); 
    } 
} 

public class Question2 { 


    private static int q(int x) throws ZeroEnteredException { 
     if (x==0) throw new ZeroEnteredException(" Exception : the value is 0 ") ; 
     return x ; 
    } 

    private static int s(int n) throws NegativeValueException { 
     if (n < 0) throw new NegativeValueException(" Exception : the value is negative ") ; 
     return n ; 
    } 

    public static void main(String [] args) { 
     Scanner input = new Scanner(System.in) ; 
     int num ; 
     System.out.println(" enter a number : ") ; 
     while(true) { 
      num= input.nextInt(); 
      try { 
       System.out.println(q(num)) ; 
       System.out.println(s(num)) ; 

      }catch(NegativeValueException o){ 
       System.err.println(o.getMessage()); 
      }catch(ZeroEnteredException e){ 
       System.err.println(e.getMessage()); 
      } 

     } 
    } 
} 
+1

何が質問ですか?あなたにはコンストラクタがあります。 – Carcigenicate

+0

cs2015はちょうどプログラミングを始めたようです。 @ Cs2015は最初にコンストラクタが何であるかを理解しています。コード<<<< public ZeroEnteredException(String s){ super(s); } >>>コンストラクタ – Acewin

+0

のありがとう私は本当に請負業者が何であるかを理解してください:)が、私は再びコードを解決した後、私はここに質問を編集するのを忘れ:) –

答えて

1

私はあなたの質問を完全に理解していませんでしたら、私には教えてください。

まず、例外をスローした値を含む変数に例外を追加する必要があります。また、この値にアクセスするメソッドも必要です。これと同じように:例外をスローするときに行われていること

class NegativeValueException extends Exception { 
    // This has to be private. 
    private int value; 

    //This will be the expected constructor 
    public NegativeValueException(String s, int value) { 
     super(s); 
     this.value = value; 
    } 

    // We need this to know the value which triggered the exception. 
    // We don't need to do it for the message because it already exists from the exception class. 
    public int getValue() { 
     return value; 
    } 
} 

、あなたは今、値を指定することができます。

throw new NegativeValueException("Exception : the value is negative", n) ; 

そして最後に、あなたはこのようなのtry-catchブロックを記述する場合があります

try { 
    // Some code that might throw your exceptions. 
} catch (NegativeValueException e){ 
    // You enter here when a NegativeValueException is thrown. 
    // This will display your exception message. 
    System.out.println(e.getMessage()); 
    // This will display the number that threw the exception. 
    System.out.println(e.getValue()); 
} catch (ZeroEnteredException e) { 
    // You enter here when a ZeroEnteredException is thrown. 
} catch (Exception e) { 
    // You enter here when an other exception is thrown. 
    e.printStackTrace(); 
} 

サイドノート:最も具体的な例外から、キャッチブロックの最も一般的な例外に進むことを忘れないでください。適合する例外タイプを持つキャッチブロックが見つかるとすぐにプログラムが停止するため、つまり、catch(Exception e)を最初に置くと、最も一般的な型でExceptionから継承されるすべての例外が存在するため、他のcatchブロックには一度も存在しません。

+0

^_ ^方法でお願いしますということですそれは本当に私を助けてくれました。 –

関連する問題