In: Computer Science
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.
class mainExcep extends Exception{
public mainExcep() {
super("Tornado: Take cover
immediately!");
}
public mainExcep(int m) {
super("Tornado: "+m+" miles away;
and approaching!");
}
}
public class TestException{
public static void main(String args[]){
try{
throw new mainExcep();
}
catch (mainExcep ex) {
System.out.println(ex.getMessage());
}
try{
throw new mainExcep(10);
}
catch (mainExcep ex) {
System.out.println(ex.getMessage());
}
}
}
*Save file as TestException.java
Sample Execution Result
Tornado: Take cover immediately!
Tornado: 10 miles away; and approaching!