Different “ try catch ” in Java

posted in: Java | 0

try – catch:

Aspects that make Java exception handling( try catch ) different:

  • An object that’s used for throwing in Java must of type that is a subclass of Throwable. By convention, however, user-defined exceptions are of type Exception, a subclass of Throwable. Therefore, if Java class throws an exception of a user-defined type MyException, then MyException should extend Exception, as below:

class MyException extends Exception { …… }

  • In Java, for all practical purposes, we can catch Exception object to catch all kind of exceptions. Because, normally we do not catch Throwable(s) other than Exception(s) (which are Errors)

                                        catch(Exception e){                                          …….                                          }

  • If a method is written so as to throw an exception, that fact must be declared in the header of the function. So if a function f() is written to throw an exception of type MyException, then “throws MyException clause must be included in the header of the function, as below:
               void f(int j) throws MyException {                                                                              // .....
               throw new MyException (// .....);}


  • An exception-throwing method can only be invoked in one of the following modes:
  1.   It must be invoked inside a try catch block and the exception must be caught; or
  2.   The calling method must re-throw that exception so that it can be caught elsewhere, possibly by the system-supplied default exception handler.
  3.   The calling method can do both of the above.
  • A parameter must be explicitly specified for the try catch block s follows:

                                 catch(MyException e) { …. } and explicitly mention an identifier, in this case e of type MyException in the parameter list of catch() even if this parameter does not get used for anything inside the definition of the try catch block.

  • Java has explicit Object-based hierarchy for exceptions 
  • In Java, there is a block called finally that is always executed after the try catch block. This block can be used to do cleanup work.
  • In Java, a new keyword throws is used to list exceptions that can be thrown by a function. In C++, there is no throws keyword; the same keyword throw is used for this purpose also.

Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking

Handling More Than One Type of Exception

A single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to try catch an overly broad exception. Consider the following example, which contains duplicate code in each of the try catch blocks:

catch (IOException ex) {
     throw ex;
catch (SQLException ex) {
     throw ex;

The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

Note: If a try catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the try catch block. Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.

Rethrowing Exceptions with More Inclusive Type Checking

This enables you to specify more specific exception types in the throws clause of a method declaration. Consider the following example:

  static class FirstException extends Exception { }
  static class SecondException extends Exception { }
  public void rethrowException(String exceptionName) throws Exception {
    try {
      if (exceptionName.equals("First")) {
        throw new FirstException();
      } else {
        throw new SecondException();
    } catch (Exception e) {
      throw e;

This examples’s try block could throw either FirstException or SecondException. Suppose you want to specify these exception types in the throws clause of the reThrowException  method declaration. Compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the try catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException:

  public void rethrowException(String exceptionName)
  throws FirstException, SecondException {
    try {
      // ...
    catch (Exception e) {
      throw e;

The compiler verifies that the type of the rethrown exception meets the following conditions:

  • The try block is able to throw it.
  • There are no other preceding catch blocks that can handle it.
  • It is a subtype or supertype of one of the try catch clause’s exception parameters.

Leave a Reply