What Is an Exception

In this section, you will learn about the Java exception and its types.

What Is an Exception

In this section, you will learn about the Java exception and its types.

What Is an Exception

Exception in Java

     

Exception are such anomalous conditions (or typically an event) which changes the normal flow of execution of a program. Exceptions are used for signaling erroneous (exceptional) conditions which occur during the run time processing. Exceptions may occur in any programming language.

Occurrence of any kind of exception in java applications may result in an abrupt termination of the JVM or simply the JVM crashes which leaves the user unaware of the causes of such anomalous conditions. However Java provides mechanisms to handle such situations through its superb exception handling mechanism. The Java programming language uses Exception classes to handle such erroneous conditions and exceptional events. 

Exception Object

In java, when any kind of abnormal conditions occurs with in a method then the exceptions are thrown in form of Exception Object i.e. the normal program control flow is stopped and an exception object is created to handle that exceptional condition. The method creates an object and hands it over to the runtime system. Basically, all the information about the error or any unusual condition is stored in this type of object in the form of a stack. This object created is called an exception object the process is termed as throwing an exception. The mechanism of handling an exception is called catching an exception or handling an Exception or simply Exception handling. 

 

As we have known that after throwing an exception it is handed off  to the runtime system that finds a possible method from an ordered list of methods to handle it.  The list of this type of methods is known as the call stack

As we have already learned that, what are the exceptions. Point to be remember here is that exceptions are not errors rather they are some abnormal conditions that aren't necessarily errors. Therefore, the process of detecting the exceptions and responding to them as well is known as Exception handling. 

Following are the advantages of Exception-handling in Java:

  • Exception provides the means to separate the details of what to do when something out of the ordinary happens from the main logic of a program. 
  • One of the significance of this mechanism is that it throws an exception whenever a calling method encounters an error providing that the calling method takes care of that error.
  • With the help of this mechanism the working code and the error-handling code can be disintegrated. 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
  • Furthermore the errors can be propagated up the method call stack i.e. problems occurring at the lower level in the chain can be handled by the methods higher up the call chain . 

Sample Code
The basic syntax to handle an Exception looks like this:

String myException()
{
try
{
return myMethod();
}
catch ( IOException e )
{
return null;
}
}

There are three types of Exceptions:

  1. Checked Exceptions - These are the exceptions which occur during the compile time of the program. The compiler checks at the compile time that whether the program contains handlers for checked exceptions or not. These exceptions do not extend RuntimeException class and must be handled to avoid a compile-time error by the programmer. These exceptions extend the java.lang.Exception class  These exceptional conditions should be anticipated and recovered by an application. Furthermore Checked exceptions are required to be caught. Remember that all the exceptions are checked exceptions unless and until those indicated by Error, RuntimeException or their subclasses. 

    For example if you call the readLine() method on a BufferedReader object then the IOException may occur or if you want to build a program that could read a file with a specific name then you would be prompted to input a file name by the application. Then it passes the name to the  constructor for java.io.FileReader and opens the file. However if you do not provide the name of any existing file then the constructor throws java.io.FileNotFoundException which abrupt the application to succeed. Hence this exception will be caught by a  well-written application and will also prompt to correct the file name.

    Here is the list of checked exceptions.

    NoSuchFieldException 

    InstantiationException 

    IllegalAccessException 

    ClassNotFoundException 

    NoSuchMethodException 

    CloneNotSupportedException 

    InterruptedException 

     
     

  2. Unchecked Exceptions -  Unchecked exceptions are the exceptions which occur during the runtime of the program. Unchecked exceptions are internal to the application and extend the java.lang.RuntimeException that is inherited from java.lang.Exception class. These exceptions cannot be anticipated and recovered like programming bugs, such as logic errors or improper use of an API. These type of exceptions are also called Runtime exceptions that are usually caused by data errors, like arithmetic overflow, divide by zero etc. 

    Lets take the same file name example as described earlier. In that example the file name is passed to the constructor for FileReader. However, the constructor will throw NullPointerException if a logic error causes a null to be passed to the constructor. Well in this case the exception could be caught by the application but it would rather try to eliminate the bug due to which the exception has occurred. You must have encountered the most common exception in your program i.e. the ArithmeticException. I am sure you must be familiar with the reason of its occurrence that is when something tries to divide by zero. Similarly when an instance data member or method of a reference variable is to be accessed that hasn't yet referenced an object throws NullPointerException.

    Here is the list of unchecked exceptions.

    IndexOutOfBoundsException 

    ArrayIndexOutOfBoundsException

    ClassCastException 

    ArithmeticException 

    NullPointerException 

    IllegalStateException 

    SecurityException

     

  3. Error The errors in java are external to the application. These are the exceptional conditions that could not be usually anticipated by the application and also could not be recovered from. Error exceptions belong to Error and its subclasses  are not subject to the catch or Specify requirement. Suppose a file is successfully opened by an application for input but due to some system malfunction could not be able to read that file then the java.io.IOError would be thrown. This error will cause the program to terminate but if an application wants then the error might be caught. An Error indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

     Hence we conclude that Errors and runtime exceptions are together called as unchecked exceptions.