Home arrow Java & J2EE arrow Page 2 - Exception Handling Techniques in Java

Exception Handling - Java

Exception handling is one of those programming techniques that beginners often neglect to implement. However, when they end up working on a large project on which dozens of other colleagues are working, they find that you just can’t afford to be superficial. Throughout this article we’re going to cover exceptions, explain what they are, how to work with them, and why they are important—ultimately, how to handle them.

  1. Exception Handling Techniques in Java
  2. Exception Handling
  3. More Exception Handling Examples
  4. Final Thoughts
By: Barzan "Tony" Antal
Rating: starstarstarstarstar / 30
March 11, 2009

print this article



On the previous page we mentioned that exception handling either deals with the exception in the code snippet where it occurs with the try/catch block, or throws the exception back to the runtime engine, which on its end will search for the exception handling routine that is the closest to the place where the exception occurred. It searches within the call stack that contains the sequence of method calls.

Generally, exceptions can happen due to either having an abnormal event that leads to an exceptional case, or another method has thrown the exception and now it's time to deal with it, or in the case of an asynchronous exception (which happens only when multiple threads are used and they aren't synchronized appropriately). Now we are going to cover the basics of exception handling: how to "catch" and deal with them.

Java allows us to create our own Exception objects and classes-but there is a critical requirement. They must be extending the Exception class, and that's how inheritance happens. It is part of the coding standard that exceptions must be named quite "thoroughly," meaning their name should speak for themselves.

throw new Exception(" This is an exception! ");


Now let's see how to catch and deal with an exception. Check out the following.


try {

// this is the block of code where the exception happens

// sometimes called as source/root of exception

// or even called as tricky block or tricky method



catch (Exception_Type1 e){

// dealing with this kind of exception


catch (Exception_Type2 e){

// dealing with this kind of exception


// ... unlimited number of catches are possible

finally {

// this block of code is always executed



The first part of the try-catch-finally construct is the try block. This is the segment where the exception may occur. Generally it's advised to write the minimum amount of code lines here since they are executed only until the exception happens. When it does, the execution jumps to the catch blocks, where the exception types are compared. If they match, then the exception that occurred is dealt with. 

The finally block is always executed, regardless of whether or not an exception happens during the try block, or whether an exception could be handled within the catch blocks. Since it always gets executed, it is recommended that you do some cleanup here. Therefore, as expected, implementing the finally block is optional.

The structure of try-catch blocks is similar to that of switch-case constructs. It should also be said that, in the case of checked exceptions, which must be dealt with, it is possible to either "handle" them right where they occur inside that same method, or throw them further. The latter can be done with the throws keyword. And in this case, the type of exception must be specified in the method signature. See the example:

void myMethod () throws SomeKindOfException{

// method goes here



On the next page we're going to present some more applicable examples. 

>>> More Java & J2EE Articles          >>> More By Barzan "Tony" Antal

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: