InterviewSolution
| 1. |
Write a Java program to create and throw custom exceptions. |
|
Answer» class InterviewBit { public static void main(String args[]) throws CustomException { // Throwing the custom EXCEPTION be passing the message throw new CustomException(" This is my custom Exception "); }}//Creating Custom Exception Classclass CustomException extends Exception{ //Defining Constructor to throw exception message public CustomException(String message){ super(message); }} We have created the exception class named with CustomException and called the BASE exception constructor with the ERROR message that we want to print. And to avoid HANDLING exceptions in the main method, we have used the throws keyword in the method declaration. |
|