Home arrow Java & J2EE arrow Exception Handling Techniques in Java

Exception Handling Techniques in 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

All right, let's begin explaining what exceptions are. Just as in other programming languages, this applies to Java as well: exceptions are those errors that occur during runtime. These aren't real errors, because they are exceptions. One might call them exceptional events that can and should be handled to continue program execution. So there's "something" one has to do about them.

One of the most remarkable examples of exceptional cases and conditions is when, during program execution, the time comes for a division by zero. This cannot be done and, therefore, Java throws an exception, specifically the ArithmeticException. From the Java programmer's point of view, exceptions are objects. Throwing exceptions is akin to throwing objects. But here's the drill: not every object can be thrown.

In order to fully understand throwable exceptions, some parts of the entire class hierarchy should be presented. There is one main class called Throwable. This class has two sub-classes: Exception and Error. An exception object must be descended from a class that is Throwable, meaning it must be an object instance of either an Exception sub-class or Error sub-class. These can both be found in the java.lang package.

Exception handling is the technique of catching the exceptions that might be thrown some time in the future during runtime. Java offers robust exception handling solutions with the try-catch-finally construct. On other hand, you can work with already-declared exceptions, such as the ArithmeticException, NullPointerException, and others. Other classes extend the Exception class-e.g., the IOException subclass from java.io

Furthermore, we should also note that exceptions are of two kinds: unchecked and checked. Unchecked exceptions are technically RuntimeExceptions (or its subclasses). These don't need to be declared in your throws clauses, and catching them is optional, but many don't bother-they occur without the knowledge of programmers, who may not even know that those are "catchable." Most of the time, these are logic programming errors such as NullPointerException or ArrayIndexOutOfBounds.

Meanwhile, checked exceptions technically force the programmer to handle and manage them, meaning catch and cover them individually. These are derived from the Exceptions class and its subclasses, excluding the RuntimeExceptions as we discussed in the paragraph above (those are unchecked!). Checked exceptions require exception handling because they might cause program termination.

Now that we've learned the basic theory, let's fire up our IDE, and start coding! 



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- 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: