MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Explaining Java exception handling mechanism

In this article we will see the explanation about Java exception handling mechanism.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Exception handling is an important aspect in any real world application. The applications which are complex in nature and handles lot of complex logic need a careful design of exception handling framework. So during design phase the exception/error handling architecture is finalized and documented for future reference. The exception handling mechanism should be flexible enough for future enhancement or modification.

Similarly in java, exception handling framework enables your applications to handle errors and exceptions in a proper way. So during design phase, all possible exceptions should be considered and handled carefully. Although exception handling is an important aspect of application development but it is often neglected during development time. As a result during production time unknown errors kill the system and produces unexpected result. So, improper exception handling is one of the major reasons for real time applications failure. And at that point it is very difficult to redesign the application and implement proper exception handling mechanism.

To prevent this java provides proper APIs for implementing exception/ error handling framework. When an error occurs in a Java program it usually throws the exception. Now the java application catches and handles these exceptions following the design. There are several different ways to handle exception and are selected based on the application requirement.

The basic purpose of exception handling is to maintain the normal flow of the application irrespective of errors/exceptions which might occur during execution. If proper exception handling is not implemented then the application flow will be disrupted. In java, exception or error are events which occur during application execution due to various abnormal scenarios. These are basically exception or error objects defined by the java run time system and handled by the exception handling framework. So the basic exception/error APIs are provided by the java environment and the application developer needs to implement it as per the requirement.

There are mainly two types of exceptions as mentioned below

  • Checked exception
  • Unchecked exception

Although some time 'Error' is described as the third type of exception but it is actually considered as unchecked exception. It is irrecoverable like VM (virtual machine) error, out of memory, system failure etc. It is considered as the “hard” failure in the virtual machine and the virtual machine throws this Error. Typical Java applications should not catch Errors and it is also unlikely that typical Java programs will ever throw Errors either. But still some time the exception handling framework should be designed carefully to avoid all types of abnormal scenarios. Examples of errors are OutOfMemoryError, LinkageError, and StackOverflowError.

Checked exceptions are exceptions that are declared in the throws clause of a method. Checked exceptions are considered at compile time so it greatly reduces the occurrence of unhandled exceptions during runtime. They extend Exception and are intended to be handled at compile time. A checked exception indicates an expected problem which might occur during normal application execution. Examples of checked exceptions are IOException; SQLException etc

Following are the list of checked exceptions

  • NoSuchFieldException
  • CloneNotSupportedException
  • InterruptedException
  • IllegalAccessException
  • ClassNotFoundException
  • InstantiationException
  • NoSuchMethodException

Unchecked exceptions do not need to be declared in a throws clause. They extend RuntimeException. An unchecked exception indicates an unexpected problem which occurs due to coding bug. These are exceptions introduced by the application developer during coding. These exceptions are not checked during compile time but they are checked during runtime. Examples of unchecked exceptions are NullPointerException, ArithmeticException etc.

Following are the list of unchecked exceptions

  • ArithmeticException
  • NullPointerException
  • IllegalStateException
  • SecurityException
  • IndexOutOfBoundsException
  • ArrayIndexOutOfBoundsException
  • ClassCastException

The following picture shows the exception class hierarchy.

Exception hierarchy

Figure 1: Exception hierarchy

Catching Exceptions and building blocks in java: The basic building blocks are try-catch-finally

Any java method catches an exception using a combination of the try and catch keywords as shown below. The try/catch block is placed around the code that might generate an exception. There might be many try/catch blocks. The catch block defines exceptions classes which the code might throw. The parent class is ‘Exception’ but the user can define any exception class inside catch block. The finally block always executes irrespective of the type of exception.

Following is a basic sample code for exception handling in java.

try
{
   // java code which might cause exception
}catch(ExceptionName  e1) // It can be any type of exception
{
   //Catch block for handling the exception
}
finally
{
   //The finally block always executes irrespective of the exception.
}

Custom Exception Handling: In most of the real time application, custom exception handler is designed to meet the application requirement. The normal exception APIs provided by the java are basic building blocks. Now the developer is expected to use these building blocks to make its own custom exception handler.

To define your own custom exception class, developer needs to extend the Exception class. All custom exceptions are considered to be checked exceptions. The custom exception class is like any other normal class but they provide useful methods and directs customized execution path

Following rules needs to be followed to define custom exception class

  • All exceptions must be a child of Throwable.
  • Must extend the Exception class for checked exception.
  • For runtime exception, extend the RuntimeException class.

You can define our own Exception class as shown below:

class MyCustomException extends Exception{
// Write useful methods and execution path
}

Following are the advantages of exception handling in java

  • By using exception code within catch block, developer can define the execution path if something wrong happens in the normal flow
  • Normal code and error handling code is completely separate, hence it provides more flexibility and the components are not tightly coupled.
  • The error can be handled at a much higher level irrespective of its occurrences
  • Normal application flow is never hampered

Conclusion:

Java exception framework is very powerful. Good exception handling framework helps an application to run smoothly. An application can use exceptions to indicate that an error has occurred and then it can be handled in a more controlled way. Basically exception handling helps an application to maintain its normal flow. Even if some unexpected error occurs, the exception framework provides separate execution path to avoid application failure. So for any robust java application the exception handling framework must be designed properly.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login