Net-informations.com
SiteMap  | About    

Exceptions in Java

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. It provide a way to transfer control from one part of a program to another. Exceptions are occurred in situations like your program run out of the memory , file does not exist in the given path , network connections are dropped etc. More specifically for better understanding , we can say it as Runtime Errors occurs during the execution of a program that disrupts the normal flow of instructions. The following image shows an outofmemory exception:

How to Java try catch exception

Exception Hierarchy in Java

Exception class is used for exceptional conditions that programs should handle. The possible exceptions in a Java program are organized in a hierarchy of exception classes. The Throwable class , which is an immediate subclass of Object, is at the root of the exception hierarchy. Throwable has two immediate subclasses: Exception and Error . There is an important subclass of Exception, known as RuntimeException.

Java exception hierarchy

Exception Handling in Java

Exception handling is all about ensuring that when your program encounters an error or exception, it will continue to run and provide informative feedback to the end-user or program administrator. The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. We need to include some specific codes in the program to handle the exceptional situation. This process of embedding codes to handle exceptional situation is known as exceptional handling. There are 5 keywords used in java for exception handling. They are:

  1. try
  2. catch
  3. finally
  4. throw
  5. throws

Syntax:

To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try{}-block . When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

Example without catch exception:

Output:

When you run the above code, the compiler throws java.lang.ArithmeticException and all the code after exception will not be executed. In this type of situations we have to use Exception Handling.

Example:

Output:

Here you can see the exception handled in the catch block and the program continue to next line. It is important to note that Java try block must be followed by either catch or finally block.

Java finally block

A finally statement must be associated with a try..catch statement. It identifies a block of statements that needs to be executed regardless of whether or not an exception occurs within the try block. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch parts of the block. In normal case execution the finally block is executed after try block. When any exception occurs first the catch block is executed and then finally block is executed.

Syntax:

Example with exception:

Output:

Here exception happened but the finally code block executed.

Example with out exception:

Output:

Here we can see the program execute without any exception, but after that finally block also executed.

How to Catch multiple exceptions

A method can throw more than one exceptions. However that method needs to declare all the checked exceptions it can throw

With release of Java 7 , oracle has done some good changes in exception handling mechanism also. Primarily these are improved catch block and redundant throws clause.

In this feature, now you can catch multiple exceptions in single catch block. Before java 7, you was restricted to catch only one.

Checked and Unchecked Exceptions

Checked Exceptions are subclasses of java.lang.Exception and Unchecked Exceptions are subclasses of java.lang.RuntimeException.

Checked Exception

Checked exceptions are checked at compile time by the JVM(Java Virtual Machine) and its related to resources(files/db/stream/socket etc). The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions . If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword. The motive of checked exception is that at compile time if the resources are not available the application should define an alternative behaviour to handle this in the catch/finally block.

Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions. Unchecked exceptions are purely programmatic errors, such as logic errors or improper use of an API, null data or even failures in business logic can lead to runtime exceptions. Runtime exceptions are ignored at the time of compilation, e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Its absolutely fine to handle/catch unchecked exceptions in code. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception . It is up to the programmers to be civilized, and specify or catch the exceptions

Advantages of Exception Handling

Java provides a sophisticated exception handling mechanism that enables you to identify exceptional conditions in your code and fix the exceptions as and when they occur. Some of important advantages as follow:

  1. Exception handling allows us to control the normal flow of the program by using exception handling in program.
  2. The ability to propagate errors up the call stack Another important advantage of exception handling in object oriented programming is the ability to propagate errors up the call stack.
  3. It throws an exception whenever a calling method encounters an error providing that the calling method takes care of that error.
  4. It also gives us the scope of organizing and differentiating between different error types using a separate block of codes. This is done with the help of try-catch blocks.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.