Question

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

  1. 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)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ExceptionTest.java

// A Class that represents use-defined expception
class MyException extends Exception
{
   public MyException(String s)
   {
       // Call constructor of parent Exception
       super(s);
   }
}

// A Class that uses above MyException
public class ExceptionTest
{
static void add(int a,int b)
{
try
       {
   if(a<0&&b<0)
   {  
           // Throw an object of user defined exception
           throw new MyException("Both the parameters passed in this method are negative");
  
   }
   System.out.println(a+b);
       }
       catch (MyException ex)
       {
           System.out.println("Exception was caught");

           // Print the message from MyException object
           System.out.println(ex.getMessage());
       }
}
   // Driver Program
   public static void main(String args[])
   {
       add(-2,-5);
   }
}
Output:

Exception was caught
Both the parameters passed in this method are negative.
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.

  • 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...

  • 4. Write a static method named ConcatStrings that is passed any number of parameters of   type...

    4. Write a static method named ConcatStrings that is passed any number of parameters of   type String, and returns a single string that is the concatenation of the strings passed.   (4 pts.)   5. Write a static method named ConcatObjects that is passed any number of parameters of   type Object, and returns a single string that is the concatenation of the result of the call to   toString() for each of the objects. (4 pts.)   6. Write a class named Triplet that...

  • With Java Language: In this assignment, you are to create a class named Payroll. In the...

    With Java Language: In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) .İd: String (5 pts) hours: int (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an...

  • File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute...

    File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...

  • 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

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