Sitemap

Java Exceptions

2 min readOct 19, 2023

--

Exception

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

Error

  • An error is also an unwanted condition but it is caused due to lack of resources and indicates a serious problem e.g. OutOfMemoryError
  • Errors are irrecoverable, they cannot be handled by the programmers.
  • They always occur at runtime (unchecked).

Exception Handling

Exception handling means gracefully defining the resolution if an exceptions occurs in the code execution flow so that program executes properly without any halt.
Java provides several ways to handle exceptions like try-catch block, throws keyword.

Exception Class Hierarchy

Checked (Compile Time) Exceptions

  • Checked exceptions are the exceptions which java makes you to deal with at compile time.
  • If checked exceptions are not handled then program will not even compile.
  • Exception class and its children classes(except RuntimeException class and its children classes) are checked exceptions.

Get Lavish Jain’s stories in your inbox

Join Medium for free to get updates from this writer.

Unchecked (Run Time) Exceptions

  • The programs with unchecked exceptions get compiled successfully but they give runtime errors if not handled.
  • RuntimeException class and its children classes are unchecked exceptions.

Custom Exceptions

Though java provides a set of predefined exceptions, it also enable users to define their own custom exceptions.

Need for custom exceptions

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

Creating Custom Exceptions in Java

To create custom exception in java, we have to extend java.lang.Exception class for checked exception and java.lang.RuntimeException for unchecked exception and provide a constructor that takes a String as the error message and calls the parent class constructor.

public class CustomException extends Exception{
}

//use it as
//throw new CustomException();

That’s it our custom exception is ready.
But still we cannot pass any exception message while throwing this exception. Let’s add one more constructor.

public class CustomException extends Exception{

public CustomException(){}

public CustomException(String message){
super(message);
}
}

//use it as
//throw new CustomException();
//throw new CustomException("exception message");

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

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");
//throw new CustomException("exception message", new RuntimeException());

--

--