Exception Handling In Java

24 April 2007

Exception handling is an integral part of programming. In Java exceptions are itself defined as objects in other words throwing an exception is throwing an object. “ Throwable ”, declared in “java.lang package” is the parent class for all the classes that a program can instantiate and throw.

Basically Throwable has two direct subclasses.

  1. Exception.
  2. Error.

Exceptions (means members of the Exception family) are thrown to indicate abnormal conditions that can often be handled by some catcher, Though there is a possibility that they may not be caught and therefore result may be a dead thread.

Errors (members of the Error family) are usually thrown for more serious problems like OutOfMemoryError. That may not be easy to handle in programs so Errors are usually thrown by the methods of the Java API, or by the JVM itself. So in general, the code you write should throw only exceptions, not errors. Errors cannot be caught.

The keywords reserved for the exception handling part in Java is listed below.

Exceptions thrown by “ throws ” clause can further be handled in “ try-catch ” block of the caller method.
RunTimeException is not mandatory to be thrown, As Java API itself throws such exceptions in many situations like arithmetic errors, illegal argument passed to method etc.
As per the business needs programmers can declare their own exceptions which are inherited from Exception class

Here is a sample program for exception handling.

public class CalculationException extends Exception {
 
public CalculationException(String msg){
 
 	super(msg);
 
 }
 
}
 
public class ExceptionTest {
 
int divide(int num,int div) throws CalculationException{
 
 	if(div==0)
 
 		throw new CalculationException("can not divide by zero");
 
return num/div;
 
 }
 
 public static void main(String[] args) {
 
 	ExceptionTest test = new ExceptionTest();
 
 	try {
 
 		System.out.println(test.divide(2,0));
 
 	} catch (CalculationException e) {
 
 		e.printStackTrace();
 
 	}
 
 }
 
}

del.icio.us:Exception Handling In Java  digg:Exception Handling In Java  spurl:Exception Handling In Java  wists:Exception Handling In Java  simpy:Exception Handling In Java  newsvine:Exception Handling In Java  blinklist:Exception Handling In Java  furl:Exception Handling In Java  reddit:Exception Handling In Java  fark:Exception Handling In Java  blogmarks:Exception Handling In Java  Y!:Exception Handling In Java  smarking:Exception Handling In Java  magnolia:Exception Handling In Java  segnalo:Exception Handling In Java  gifttagging:Exception Handling In Java

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Exception Handling In Java

One Response to “Exception Handling In Java”

  1. Christian Ullenboom Says:

    One can add that using an unchecked exception is no longer evil. So on can model CalculationException as well as a subclass of RuntimeException. I think it’s although a good idea to use a proper base class like UnsupportedOperationException (a RuntimeException) for own exceptions.

    Christian

Leave a Reply