Write a Java program to throw user-defined exception 'Number is too small'.
class MyException2 extends Exception {
public MyException2(String message) {
super(message);
}
}
public class TestMyException {
public static void main(String[] args) {
int x = 5, y = 1000;
try {
float z = (float) x / (float) y;
if(z < 0.01) {
throw new MyException2("No. is too small");
}
}
catch (MyException2 e) {
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally {
System.out.println("I am always here");
}
}
}
Caught my exception
No. is too small
I am always here