*Java* 1. Define the exception class called TornadoException . The class should
ID: 3726530 • Letter: #
Question
*Java*
1. Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return
"Tornado: Take cover immediately!"
The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return
"Tornado: m miles away; and approaching!"
Write a Java program to test the class TornadoException
Explanation / Answer
import java.io.*;
import java.lang.Exceptions;
class TornadoException extends Exception
{
int n;
public TornadoException(int m)
{
m=n;
System.out.println("Tornado:m miles away; and approaching!");
}
}
// A Class that uses above MyException
public class Main
{
// Driver Program
public static void main(String args[])
{
try
{
// Throw an object of user defined exception
throw new TornadoException("Tornado:Take cover immediately");
throw new TornadoException(1);
}
catch (TornadoException ex)
{
System.out.println("Caught");
// Print the message from TornadoException object
System.out.println(ex.getMessage());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.