Exception in Java
In terms of dictionaries, an exception is an abnormal condition. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
In Java, an exception is an event that disrupts the normal flow of a program.
When we execute our program, sometimes an error is generated for some of reasons given below, when this error is generated during execution it is called an exception. The exception can occur:
To understand exceptions in Java, you need to understand the two categories of exceptions; they are:
Checked Exception
It is also called a compile-time exception since they checked at compile time of the program. For example, ServletException, ClassNotFoundException, NoSuchFieldException etc.
Unchecked Exception
It is also called a run-time exception since they occur during run-time. Such an exception is not checked at compile time, in other words NullPointerException, OutOfMemoryError, etc.
example
public class RunTimeExcpn { public static void main(String[] args) { String str = null; int len = str.length(); } }
Output:
Now the subject arises of how to overcome this problem of exception, for that Java provides am exception handler technique. Now we discuss exception handling in Java.
Exception Handling in Java
It is a powerful feature provided in Java. It is a mechanism to handle run-time errors, so that the normal flow of the program is maintained.
Exception handling consists of the following five keywords:
Now, we show an example for exception handling:
class Demo { public static void main(String args[]) { try { int data=50/0; } catch(ArithmeticException e) { System.out.println(e); } System.out.println("remaining code"); } }
Output:
In this program an arithmetic exception occurs that is sometimes called “divide by null error” problem, for handling this exception we use a “try-catch block”. In the try block we write the code where the problem occurs and in the catch block we catch the exception as determined by its type. In this program the arithmetic exception is generated so we use a catch(ArithmeticException e) statement to maintain the flow of the program.
We are not going into the details of Exception Handling; our topic is “multiple exceptions in Java 7“. So for discussing our topic we take a short look at “exception” and “exception handling” in Java.
Multiple Exception in Java 7 new concept
It provides a way of “Handling More Than One Type of Exception“. Java 7 made it possible to catch various exceptions in the same catch block that is not possible in prior versions of Java. This is also known as multi-catch.
In prior versions of Java:
catch (IOException ey) { logger.log(ey); throw ey; catch (SQLException ey) { logger.log(ey); throw ey; } catch(Exception e) { logger.severe(e); }
In prior versions of Java 7, it is difficult to create a common method to remove the repeated code because the variable “ey” has various types.
The following example is valid in Java 7 and later, it eliminates the need for repeated code:
catch (IOException | SQLException ey) { logger.log(ey); throw ey; }
The catch defines the types of exceptions, and each exception type is separated with a vertical bar ( | ).
Advantages of Multiple Exception in Java 7
Now in following example we show a difference between exception handling in Java 7 and in prior versions of Java.
Exception handling in Java
class ExceptionEx1 { public static void main(String args[]) { try { int a[]=new int[5]; a[5]=345/0; } catch(ArithmeticException e) { System.out.println("work 1 done"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("work 2 completed"); } catch(Exception e) { System.out.println("Common task completed"); } System.out.println("remaining part of the code"); } }
Output:
Exception handling in Java 7
class ExceptionEx2 { public static void main(String args[]) { try { int a[]=new int[5]; a[5]=345/0; } catch(ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println("work 1 done"); } catch(Exception e) { System.out.println("Common task completed"); } System.out.println("remaining part of the code"); } }
Output:
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…