First we need to understand the Exception mechanism in java before going to customize it.
Details regarding Exceptions in Java available @Exceptions in Java.
If we create our new exception that we can call as Custom or User Defined Exception. We can customize the exception as per our need. With Custom Exception feature, we can have our own exceptions and message.
So here we can customize both Checked(Exception) and Unchecked Exceptions(Runtime Exception).
If client is not going to recover or can't to anything from Exception, then we can go for Unchecked Exception. I mean if client is not doing any action on exception and simply logging the exception, we can go for Unchecked exception.
Or
If Client is going to recover from Exception then we can go for Checked Exception.
We need to do some analysis and identify whether we really need to go for custom exceptions which are not given by JAVA platform before simply doing customization.
And also user should be benefited somehow with custom exceptions.
As it seems it is very easy to create exception by extending Exception class, we need to follow some best practices to customize the exceptions:
1. Add at least two more constructors apart from default constructor in custom exception class to specify throwble and failure messages.
2. If custom exception is creating by passing another exception then consists original exception as source. We can use constructors to take Exception rather than only messages.
3. For readability add the Exception at end Of custom exception class name like "NameNotFoundException".
4. Plan to keep Exception is very light control over an application. Do not control the behavior of application with custom exception.
5. Mostly try to use exception classes given by java platform until unless you do not have any specific requirement. Because Exception handling is very expensive as it requires to make native calls to copy stack trace for exception each time Exception is created.
Checked Exception :
1. we can create custom checked Exception by extending Exception class.
2. It is for recoverable condition
3. Need to put try catch block
4. This is compile time exception
5. IOException, FileNotFoundException etc are Checked Exceptions
Unchecked Exception :
1. We can create Unchecked custom exception by extending Runtime Exception
2. When we just showing error message in logs
3. No try catch block required.
4. This is runtime Exception
5. NullPointerException, IndexOutOfBoundException etc are Unchecked Exceptions
We will see Examples for Custom Exceptions in next post.
Details regarding Exceptions in Java available @Exceptions in Java.
If we create our new exception that we can call as Custom or User Defined Exception. We can customize the exception as per our need. With Custom Exception feature, we can have our own exceptions and message.
So here we can customize both Checked(Exception) and Unchecked Exceptions(Runtime Exception).
If client is not going to recover or can't to anything from Exception, then we can go for Unchecked Exception. I mean if client is not doing any action on exception and simply logging the exception, we can go for Unchecked exception.
Or
If Client is going to recover from Exception then we can go for Checked Exception.
We need to do some analysis and identify whether we really need to go for custom exceptions which are not given by JAVA platform before simply doing customization.
And also user should be benefited somehow with custom exceptions.
As it seems it is very easy to create exception by extending Exception class, we need to follow some best practices to customize the exceptions:
1. Add at least two more constructors apart from default constructor in custom exception class to specify throwble and failure messages.
2. If custom exception is creating by passing another exception then consists original exception as source. We can use constructors to take Exception rather than only messages.
3. For readability add the Exception at end Of custom exception class name like "NameNotFoundException".
4. Plan to keep Exception is very light control over an application. Do not control the behavior of application with custom exception.
5. Mostly try to use exception classes given by java platform until unless you do not have any specific requirement. Because Exception handling is very expensive as it requires to make native calls to copy stack trace for exception each time Exception is created.
Checked Exception :
1. we can create custom checked Exception by extending Exception class.
2. It is for recoverable condition
3. Need to put try catch block
4. This is compile time exception
5. IOException, FileNotFoundException etc are Checked Exceptions
Unchecked Exception :
1. We can create Unchecked custom exception by extending Runtime Exception
2. When we just showing error message in logs
3. No try catch block required.
4. This is runtime Exception
5. NullPointerException, IndexOutOfBoundException etc are Unchecked Exceptions
We will see Examples for Custom Exceptions in next post.
No comments:
Post a Comment