Exception & Error in Java

What is an exception?

  1. An exception is generally an unwanted event that interrupts the normal flow of the program.
  1. They can occur at both compile time (checked) and runtime (unchecked)

What is an error?

  1. An error is also an unwanted condition but it is caused due to lack of resources that indicates a serious problem. Like outOfMemoryError.
  1. Errors are irrecoverable, they can not be handled by the programmers.
  1. Errors always occur at runtime that are unchecked exceptions.

What is exception handling?

Exception handling means gracefully defining the resolution if an exception occurs in the code execution flow so that programmers execute properly without any halt.

Java provides several ways to handle exceptions like try-catch blocks and throws keyword.

Checked (compile time) exception

  1. Checked exceptions are the exceptions which Java makes you deal with at compile time.
  2. If checked exceptions are not handled then the program will not even compile.
  3. Exception class and its children class( except RuntimeException class and its children)
    IoException
    SqlException
    ClassNotFoundException
    FileNotFoundException
    they are type of checked exception

Are checked exceptions

Unchecked (runtime) exception

  1. The programmers with runtime exceptions get compiled successfully but they give runtime errors if not handled.
    NullPonterException
    ArrayIndexOutOfBoundException
    ArthmiticException
    They are type of runtime exception.
  2. Runtime Exception class and its children classes are unchecked exceptions.

Custom Exceptions

Java provides a set of predefined exceptions, it also enables users to define their own custom exceptions. 

What is the need for custom exceptions?

  1. Sometimes there is a need to have a business logic specific exception that gives greater understanding of the problem to application users and developers. 
  2. To catch and provide specific treatment to a subset of existing Java exceptions.

How to create custom exceptions?

To create custom exceptions in java, we have to extend the java.lang.Exception class for checked exceptions.

java.lang.RunTimeException for unchecked exceptions and provides a constructor that takes a String as the error message and calls the parent class constructor. 

That’s all our custom exception is ready.

But still, we can not pass any message while throwing this exception. Let’s add one more constructor. 

We can also add a constructor in which we can pass the Exception that has caused this particular problem.

public class CustomException extends Exception{

   public CustomException(){

   }

   public CustomException(String message){

       super(message);

   }

   public CustomException(String message, Throwable cause){

       super(message, cause);

   }

}

//use it as

//throw new CustomException();

//throw new CustomException(“exception message”);

Difference between ClassNotFoundException and NoClassDefFoundError exception

ClassNotFoundException occurs at runtime.

NoClassDefFoundError occurs when class was present at compile time but missing at runtime.

Author: Susheel kumar

Leave a Reply

Your email address will not be published. Required fields are marked *