Definition

  • An exception is a condition that arise a problem, which stops program execution suddenly,
    from continuation from the point of occurrence of it.

Reason/Cause of Exception

  • Exception arises at run time due to some abnormal condition arise in the program. For example, when a method contains code which produces “divide by zero” situation in the program at run time then this method may throw an exception.
  • Also, if a program written in Java does not follow the rule of Java language correctly or violates the Java execution environment, constraints exception may occur.

Precaution for Exception

  • When an exception is caught in the program, there are several things that can be done:
    • Problem/Exception must be fixed which is first and major thing.
    • Do something else to manage the problem instead to avoid the problem/exception.
    • If not manageable, exit the application using System.exit() method.
    • If possible, Re-throw the exception to some other method or portion of code that does not produce fatal error condition.
    • If not handle, throw a new exception to replace previous one.
    • If nothing look, return a default value/situation (a non-void/return type method – traditional way of handling exceptions).
    • Engulf the exception in less important method and finally come out from the method (in a void method).
    • Don’t give importance to the exception if they are not so fatal for our program.
    • Engulf the exception and continue in the same method as normal, if the program is not so sensitive or important(Rare but dangerous to use it).

Features/Characteristics

  • In C++ , there is a very basic provision for exception handling not as in java.
  • Java run time system automatically throws system-generated exceptions.

Handling of Exceptions

  • Exceptions in Java are handled by the use of five keywords completely : try, catch, throw,
    throws, and finally

The try block

  • Generally we put main programming statements/codes of program on which we want to monitor for exceptions, in try block.

The catch block

  • If any exceptions occur in try block that will be generally/specifically catched using catch block.
  • Multiple Catch
    • Sometimes there is a chance to have multiple exceptions arise in a block of code of a program. For this, we can use multiple catch clauses to catch the different kinds of exceptions that the code can throw. To handle these exceptions more than one catch clauses/area are used. When an exception is thrown,different catch blocks associated with try block checks the thrown order and identify the first, one
      whose type (the exception type passed as argument in catch clause) matches with the exception type is executed.

The finally block

  • Code written in this block are always executed even if no exceptions.
  • It may be optional for a program.
  • A finally clause is included in a program in the last block of execution in the program.
  • If an exception is either thrown or not and if they are not caught inside the catch block, it could exit the catch block and ultimately reach the finally block.

Working Mechanism – To catch an exception successfully in Java, we write code inside a try block with one or more catch clauses as per need. Here, each catch clause specifies one exception type to handle it properly. If an error arise in the try block code at run time it throws an exception, the associated catch clauses will be examined by the Java virtual machine. If the virtual machine finds a catch clause that is prepared to handle the thrown exception, the program continues execution starting with the first statement of that catch clause, and the catch block is used for executing code to handle exception and graceful termination of the program.

Syntax

Advantage

  • Basically exception handling provides a safe escape route from problem or clean-up
    of error handling code.

Disadvantage

  • Exception stops the program to continue further abnormally(if not handle properly) as it arise because of lack of information to deal with the exception condition.

Types of Exception Handling

  • There are two types of exception in java –
    • Checked Exception
    • Unchecked Exception
  • Checked Exception –
    • When Java compiler and the JVM both checks the java source code to make sure that the rules of Java are obeyed fully or not, is called checked exception.
    • Checked exceptions must be handled at compile time.
    • Checked exceptions are those that cannot be ignored rather handled it properly otherwise program will terminate abnormally.
    • All exceptions instantiated from the Exception class or its sub classes are of this nature.
    • Examples are – Environmental error( that cannot necessarily be detected during testing such as disk full, broken socket, database unavailable, etc), virtual machine error (such as Class not found, out of memory, no such method, illegal access to private field etc).
  • Unchecked/Runtime Exception –
    • Programming errors that is detected during testing of java program are of this nature.
    • For example – index out of bounds, null pointer, illegal argument, etc. are known as run time exception.
    • Run time exceptions do need to be handled and is handlable, but errors often cannot be handled.
    • Most of the run time exceptions are thrown by the Java virtual machine itself.
    • They are the members of the RuntimeException family.
    • These exceptions are usually an indication of software bugs.
    • Basically, it is those type of exception in which we have an option to handle it, or ignore it. When we select to ignore the possibility of an unchecked exception, then, as a result of that the program will terminate. When  we select to handle an unchecked exception that occur then the result will depend on the code that we
      have written to handle the exception.
    • All exceptions instantiated from the RuntimeException class or its sub classes are of this nature.
    • Examples are – Exceptions instantiated from Run time Exception and its sub classes are considered as unchecked exceptions.

List of Important Exceptions & their Reason

Slno Exception Name Examples
01. ArithmeticException Division by zero or some other kind of
arithmetic problem
02. ArrayIndexOutOfBoundsException An array index is less than zero or
greater than or equal to the array’s
length
03. FileNotFoundException Reference to a file that cannot be found
04. IllegalArgumentException Calling a method with an improper
argument
05. IndexOutOfBoundsException An array or string index is out of
bounds
06. NullPointerException Reference to an object that has not been
instantiated
07. NumberFormatException Use of an illegal number format, such
as when calling a method
08. StringIndexOutOfBoundsException A String index is less than zero or
greater than or equal to the String’s
length
09.

Exception Hierarchy

  • All exceptions that occur in Java programs are a subclass of built–in class
    Throwable. Two classes Exception and Error are subclass of Throwable class. Exception class is used to handle exceptional conditions. Error class defines those exceptions which are not expected by the programmer to handle.
  • class java.lang.Object
    |
     class java.lang.Throwable
    |
    class java.lang.Exception + class java.lang.Error

 368 total views,  2 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.