简体   繁体   中英

How can I code any other exception that could happen?

the question ask me to define two exceptions: ZeroEnteredException and NegativeValueException . Both of them have a private variable to indicate which value caused the exception to be thrown. Also define proper constructors for both. " how can i put the constructor here ? "

the program must also take care of any other exception that would happen. Use proper catch blocks as needed. " what do i use frothier exception that may happen ? "

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());
            }

        }
    }
}

Not sure I perfectly understood your question, if not please let me no.

So, first of all, you need to add a variable to your exception that will contain the value which threw the exception, and a method to access this value. Like so:

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;
    }
}

That being done, you can now specify the value when throwing the exception :

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

And finally, you might want to write a try-catch bloc like this one :

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();
}

Side note : don't forget to go from the most specific exception, to the most generic exception with your catch bloc. As the program will stop as soon as it encounters a catch block with an exception type that fits. Meaning, if you put the catch (Exception e) first, it will always go there and never in any of the other catch blocks, because it's the most generic type and all exceptions inherit from Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM