We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32
EXCEPTION HANDLING IN JAVA
• An exception in java programming is an abnormal situation that is araised during
the program execution. • In simple words, an exception is a problem that arises at the time of program execution. • When an exception occurs, it disrupts the program execution flow. When an exception occurs, the program execution gets terminated, and the system generates an error. • We use the exception handling mechanism to avoid abnormal termination of program execution. • The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error. HIERARCHY OF JAVA EXCEPTION CLASSES TYPES OF JAVA EXCEPTIONS • There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as the unchecked exception. According to Oracle, there are three types of exceptions: 1. Checked Exception 2. Unchecked Exception 3. Error Difference between Checked and Unchecked Exceptions Checked Exception • The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time. • An exception that is checked by the compiler at the time of compilation is called a checked exception. Unchecked Exception • The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime. • An exception that can not be caught by the compiler but occurrs at the time of program execution is called an unchecked exception. Error • Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc. CHECKED EXCEPTIONS • The checked exception is an exception that is checked by the compiler during the compilation process to confirm whether the exception is handled by the programmer or not. • If it is not handled, the compiler displays a compilation error using built-in classes. • The checked exceptions are generally caused by faults outside of the code itself like missing resources, networking errors, and problems with threads come to mind. • The following are a few built-in classes used to handle checked exceptions in java. 1. IOException 2. FileNotFoundException 3. ClassNotFoundException 4. SQLException 5. DataAccessException 6. InstantiationException 7. UnknownHostException • The checked exception is also known as a compile-time exception. • In the exception class hierarchy, the checked exception classes are the direct children of the Exception class. EXAMPLE - CHECKED EXCEPTIONS UNCHECKED EXCEPTIONS • The unchecked exception is an exception that occurs at the time of program execution. The unchecked exceptions are not caught by the compiler at the time of compilation. • The unchecked exceptions are generally caused due to bugs such as logic errors, improper use of resources, etc. • The following are a few built-in classes used to handle unchecked exceptions in java. 1. ArithmeticException 2. NullPointerException 3. NumberFormatException 4. ArrayIndexOutOfBoundsException 5. StringIndexOutOfBoundsException • In the exception class hierarchy, the unchecked exception classes are the children of RuntimeException class, which is a child class of Exception class. • The unchecked exception is also known as a runtime exception. EXAMPLE - UNCHECKED EXCEPTIONS UNCAUGHT EXCEPTIONS IN JAVA • In java, assume that, if we do not handle the exceptions in a program. In this case, when an exception occurs in a particular function, then Java prints a exception message with the help of uncaught exception handler. • The uncaught exceptions are the exceptions that are not caught by the compiler but automatically caught and handled by the Java built-in exception handler. • Java programming language has a very strong exception handling mechanism. It allow us to handle the exception use the keywords like try, catch, finally, throw, and throws. • When an uncaught exception occurs, the JVM calls a special private method known dispatchUncaughtException( ), on the Thread class in which the exception occurs and terminates the thread. • The Division by zero exception is one of the example for uncaught exceptions. • In the above example code, we are not used try and catch blocks, but when the value of b is zero the division by zero exception occurs and it caught by the default exception handler. JAVA EXCEPTION KEYWORDS • In java, the exception handling mechanism uses five keywords namely try, catch, finally, throw, and throws. TRY AND CATCH IN JAVA • In java, the try and catch, both are the keywords used for exception handling. • The keyword try is used to define a block of code that will be tests the occurence of an exception. • The keyword catch is used to define a block of code that handles the exception occured in the respective try block. • The uncaught exceptions are the exceptions that are not caught by the compiler but automatically caught and handled by the Java built-in exception handler. • Both try and catch are used as a pair. Every try block must have one or more catch blocks. EXAMPLE-1 EXAMPLE-2 PROBLEM WITHOUT EXCEPTION HANDLING MULTIPLE CATCH CLAUSES • In java programming language, a try block may has one or more number of catch blocks. That means a single try statement can have multiple catch clauses. • When a try block has more than one catch block, each catch block must contain a different exception type to be handled. • The multipe catch clauses are defined when the try block contains the code that may lead to different type of exceptions. • The try block generates only one exception at a time, and at a time only one catch block is executed. • When there are multiple catch blocks, the order of catch blocks must be from the most specific exception handler to most general. • The catch block with Exception class handler must be defined at the last. EXAMPLE-1 EXAMPLE-2 NESTED TRY STATEMENTS • The try block within a try block is known as nested try block in java. • When there are nested try blocks, each try block must have one or more seperate catch blocks. THROW, THROWS, AND FINALLY KEYWORDS IN JAVA throw keyword in Java • The throw keyword is used to throw an exception instance explicitly from a try block to corresponding catch block. That means it is used to transfer the control from try block to corresponding catch block. • The throw keyword must be used inside the try blcok. When JVM encounters the throw keyword, it stops the execution of try block and jump to the corresponding catch block. • Using throw keyword only object of Throwable class or its sub classes can be thrown. • Using throw keyword only one exception can be thrown. • The throw keyword must followed by an throwable instance. throws keyword in Java • The throws keyword specifies the exceptions that a method can throw to the default handler and does not handle itself. That means when we need a method to throw an exception automatically, we use throws keyword followed by method declaration • When a method throws an exception, we must put the calling statement of method in try-catch block. finally keyword in Java • The finally keyword used to define a block that must be executed irrespective of exception occurence. • The basic purpose of finally keyword is to cleanup resources allocated by try block, such as closing file, closing database connection, etc. • Only one finally block is allowed for each try block. • Use of finally block is optional. BUILT-IN EXCEPTIONS IN JAVA • The Java programming language has several built-in exception class that support exception handling. Every exception class is suitable to explain certain error situations at run time. • All the built-in exception classes in Java were defined a package java.lang. CREATING OWN EXCEPTIONS IN JAVA • The Java programming language allow us to create our own exception classes which are basically subclasses built-in class Exception. • To create our own exception class simply create a class as a subclass of built-in Exception class. • We may create constructor in the user-defined exception class and pass a string to Exception class constructor using super(). We can use getMessage() method to access the string.