Exception is runtime errors.

Exception flow

  1. An exception occurs.
  2. Searches to locate the matching catch block.
  3. Matching catch block is located, that block would be executed.
  4. Matching catch block not found, goes into the caller method where the process of matching the catch block is performed, until the matching catch block is found.
  5. If the match is not found, then the program gets terminated in the main method.

<aside> 💡

Try to handle exception, let the program continue. Else, app might crashed / bringing bad UX.

</aside>

Custom Exception

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 Class
class CustomException extends Exception{
	//Defining Constructor to throw exception message
	public CustomException(String message){
		super(message);
	}
}

throw vs throws

throw

throw