June 30, 2014

User defined or customized unchecked and checked exceptions in java

"An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions"


When do you require custom checked exception & How

Java platform provides a lot of exception classes you can use or you can write one of your own. You should write your own exception classes.
  • This establishes a set of exceptions that must be formally acknowledged in the program one way or another.
  • A checked exception is an exception that must be either caught and handled or listed in the throws clause of any method that may throw or propagate it.
  • When you want to handle exception in central place for each layer or for entire application, You need to write custom exception.
  • Now exception can be again type of checked and unchecked.
  • You need to write custom checked exception when you want to propagate certain exception handling responsibility to the callee method rather than handling it to the place where exception is generated.
  • For that you need to write throws clause hence callee method enforced to handle it or it again can pass on this responsibility to its callee method and so on.
  • This kind of enforcement is checked by JVM at compile time only when thrown exception is of type checked, Otherwise for unchecked exception JVM don't bother.
  • If JVM found that callee method is not handling or passing on the checked exception, It will throw compile time error for that.
  • Custom checked exception usually used ot indicate critical error signal, normally application need to terminated after that. For example, FileConfigurationException(fileConfig file not found scenario).

Create custom checked or compile-time exception

// custom checked exception class
public class FileConfigurationException extends Exception {
   FileConfigurationException(String errorMsg) {
       super(errorMsg);
   } 
}

 

When do you require custom unchecked exception & How

Java programming language does not require methods to catch or to specify unchecked exceptions (RuntimeException, Error, and their subclasses), programmers may be tempted to write code that throws only unchecked exceptions or to make all their exception subclasses inherit from RuntimeException. 
  • When you want to handle exception in central place for each layer or for entire application, you need to write custom exceptionnow exception can be again type of checked and unchecked.
  • A java exception that does not need to be caught or dealt with if the programmer so chooses.
  • Unchecked exceptions can be ignored completely in the code if desired.
  • Custom unchecked exception is usually used to indicate normal or acceptable error signal. For example: FileValidationException, FileServiceException(Invalid emalid, employee already exists etc…scenarios)
  • You need to write custom unchecked exception when you want to propagate certain exception handling responsibility to the callee method rather than handling it to the place where exception is generated.
  • For that you don't need to write throws clause as Unchecked exception are not checked by JVM at compile time.

Create custom unchecked or runtime exception

// custom unchecked or runtime exception class
public class FileServiceException extends RuntimeException {
   FileServiceException(String errorMsg) {
       super(errorMsg);
   } 
}

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts