Question

Write an exception class that can be thrown when two negative numbers are passed to a...

Write an exception class that can be thrown when two negative numbers are passed to a method and write an ExceptionTest class to show that the exception class works.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks!
===========================================================================

// Exception class
//Write an exception class that can be thrown when two negative numbers are passed to a method
public class NegativeNumberException extends Exception {

    public NegativeNumberException() {
    }

    public NegativeNumberException(String message) {
        super(message);
    }
}

=============================================================================

public class ExceptionTest {


    public static void checkNumbers(int first, int second) throws NegativeNumberException {

        // check both numbers are negative if true then we throw NegativeNumber exception
        if (first < 0 && second < 0) {
            throw new NegativeNumberException("Error: " + first + " and " + second + " are both negative numbers.");
        }
        System.out.println("First Number = " + first);
        System.out.println("Second Number = " + second);
    }

    public static void main(String[] args) {


        try {
            checkNumbers(1, 2); // this method is fine it will print both the numbers
            checkNumbers(1, -2); // this method is fine it will print both the numbers
            checkNumbers(-1, -2); // this method will throw exception
        } catch (NegativeNumberException ne) {
            System.out.println(ne.getMessage());
        }

    }
}

==================================================================

Add a comment
Know the answer?
Add Answer to:
Write an exception class that can be thrown when two negative numbers are passed to a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an exception class that can be thrown when two negative numbers are passed to a...

    Write an exception class that can be thrown when two negative numbers are passed to a method and write an ExceptionTest class to show that the exception class works. (5 pts) 6. Write an exception class that can be thrown when two negative numbers are passed to a method and write an ExceptionTest class to show that the exception class works. (5 pts)

  • Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...

    Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...

  • (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

    (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

  • Write a Java class (name it TwoNumbers) that will manage two numbers. The class should have...

    Write a Java class (name it TwoNumbers) that will manage two numbers. The class should have two integer variables. The class should have the following constructors: - TwoNumbers(); - TwoNumbers(3, 4); The first constructor should initialize the class variables to zero. The second constructor should initialize the class variables to the passed variables. The class should have the following methods: - setNumbers(3, 4); - setFirstNum(3); - setSecondNum(4); - getSum(); - getDiff(); - getProd(); The setNumbers(3, 4) method should initialize the...

  • Out of all the exceptions below, which exception would be thrown by the read() method of...

    Out of all the exceptions below, which exception would be thrown by the read() method of the InputStream class? Exception None of the answers are correct InException FileNotFoundException ReadException

  • What is exception propagation? Give an example of a class that contains at least two methods,...

    What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.

  • Write a non-void method named sum that is passed two int parameters when it is called....

    Write a non-void method named sum that is passed two int parameters when it is called. It then uses a for-loop to compute and return the value of the sum of all the numbers between the value of the first parameter and the second, inclusive. This method does not display anything. Give an example of how this method would be called, to produce output

  • please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in...

    please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in file that contains only positive numbers and outputs to the screen the sum of all numbers. Create your own exception class that describes the situation of a non-positive number. In your program, use this exception to handle this situation. Use other exception classes from the Java standard library to handle other I/O situations.

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT