Monday, 6 August 2012

Creating user defined exceptions in Java

Though Java provides an extensive set of in-built exceptions, there are cases in which we may need to define our own exceptions in order to handle the various application specific errors that we might encounter.


While defining an user defined exception, we need to take care of the following aspects:
  • The user defined exception class should extend from Exception class.
  • The toString() method should be overridden in the user defined exception class in order to display meaningful information about the exception.
Let us see a simple example to learn how to define and make use of user defined exceptions.

Sunday, 5 August 2012

Exception Handling : Throws clause in Java

The throws is used for specifying the exceptions thrown by a method.If a method doesn't catch the checked exceptions that can occur within it, the method must specify that it can throw these exceptions.

If a method is throwing a checked exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.

Let us take one method divide, here we are throwing a user-defined exception if the second parameter is zero.



static int divide(int first, int second) {
if (second == 0)
throw new MyException("can't be divided by zero");
return first / second;
}


In the above method you can handle the exception in two ways.


Difference between throw and throws in Java?

Throw is used to actually throw the exception, whereas throws is used for specifying the exceptions thrown by a method .They are not interchangeable. 
public void myMethod(int param) throws MyException



   if (param < 10) 
throw new MyException("Too low!); 
      --------
}
The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method. 
If a method is throwing a checked exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.

How to Throw Exceptions in Java

It is possible for your program to throw an exception explicitly, using the throw statement. The general form of throw is shown here:


throw ThrowableInstance;


Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable. Simple types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions.There are two ways you can obtain a Throwable object: using a parameter into a catch clause, or creating one with the new operator.


The flow of execution stops immediately after the throw statement; any subsequent statements are not executed. The nearest enclosing try block is inspected to see if it has a catch statement that matches the type of the exception. If it does find a match, control is transferred to that statement. If not, then the next enclosing try statement is inspected, and so on. If no matching catch is found, then the default exception handler halts the program and prints the stack trace.


Here is a sample program that creates and throws an exception. 

What is finally block in java?

The Finally block in java is used along with the try-catch statements. The usual structure is 

try {
 .....
} catch (Exception e) {
 ....
} finally {
 .....
}

 When a finally block is defined, this is guaranteed to execute, regardless of whether or not an exception is thrown. See the example given below,


public class MyFinallyBlock {
public static void main(String[] a) {
/**
* Exception will occur here, after catch block  the contol will goto
* finally block.
**/
try {
int i = 10 / 0;
} catch (Exception ex) {
System.out.println("Inside 1st catch Block");
} finally {
System.out.println("Inside 1st finally block");
}
/**
* In this case exception won't, after executing try block  the contol
* will goto finally block.
**/
try {
int i = 10 / 10;
} catch (Exception ex) {
System.out.println("Inside 2nd catch Block");
} finally {
System.out.println("Inside 2nd finally block");
}
}
}