Write a program to create your own Exception subclass. You need to also override toString() method to define a tailor made description of your own Exception subclass. Then create a class where an exception of the created Exception subclass is thrown by using throw keyword. You need to define a try and catch block to handle the exception in the main method. Finally, after the exception is handled, print "Exception Handling Completed".
import java.io.*;
class ExceptionHandle
{
void display() throws Exception
{ System.out.println("parent class"); }
}
public class Main extends ExceptionHandle {
void display() throws Exception
{ System.out.println("Exception handling completed "); }
public static void main(String[] args)
{
try {
ExceptionHandle s=new Main();
s.display();
}
catch(Exception e){}
}
}
Comments
Leave a comment