Define three new types of exceptions.
Write a class that throws three different exceptions in the three methods of the class.
Then call different methods of this class in the mian method and try to catch the exception you wrote with try catch.
public class Work2 { public static void main(String[] args) { ExceptionGenerator exceptionGenerator = new ExceptionGenerator(); // Handle the first custom compile time exception try { (); } catch (MyFirstException e) { (); } // Handle the second custom runtime exception try { (); } catch (MySecondException e) { (); } // Handle the third custom compile time exception try { (); } catch (MyThirdException e) { (); } } } class ExceptionGenerator { // Throw the first compile-time exception public void genFirstException() throws MyFirstException { throw new MyFirstException("The first compile-time exception"); } // Throw the second runtime exception public void genSecondException() { throw new MySecondException("Exception of the second runtime type"); } // Throw the second compile time exception public void genThirdException() throws MyThirdException { throw new MyThirdException("The third compile-time exception"); } } /* The first custom compile time exception */ class MyFirstException extends Exception { public MyFirstException(String msg) { super(msg); } } /* The second custom runtime exception */ class MySecondException extends RuntimeException { public MySecondException(String msg) { super(msg); } } /* The third type of custom compile time exception */ class MyThirdException extends Exception { public MyThirdException(String msg) { super(msg); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.