Ticker

6/recent/ticker-posts

Java Exception Handling with Example

Exception Handling

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Exception handling is one of the technique which is used to handle the run time error. Exception means the abnormal condition.

Exception Handling


The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. 

Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. 

        In exception there are two types 

             Checked exception 
            Unchecked Exception 

Checked Exceptions: 

The exception that extends Exception is called has checked exceptions. The checked exceptions are the exceptions that need to be handled at the compile time itself. It is compulsory or mandatory to handle the checked exception or else its a compile time error. (Please see the example for checked exception in file handling and JDBC programs) 

Eg:    ClassNotFound 
         SqlException 
         FileNotFoundException 
         IOException 
         InterruptedException 
         CloneNotSupportedException 

Unchecked Exception 

        The exception class that extends the RuntimeException are called has unchecked exceptions. It is not mandatory to handle the unchecked exceptions. 

Eg:     ArithmeticException 
          ArrayIndexOutOfBoundsException 
          NullPointerException
          NumberFormatException 


public class UnchkExp 

        public static void main(String[] args) 
        
                String s = "raghu"; 
                int i = Integer.parseInt(s);//java.lang.NumberFormatException 

                String name=null; 
                int len = name.length();//java.lang.NullPointerException 

                int a[] = new int[5]; 
                a[6]=95;//java.lang.ArrayIndexOutOfBounds Exception 
        
}

Hear the int i = Integer.parseInt(s); is going to generate the exception, but we are not handling it, so the JVM will handle it and it will terminate the program. And rest part of the program will not get executed.

So we need to handle the exception where ever it might get generated, so we can handle the exception using the try catch block. 

try

catch (Exception e) 

If we handle the exception than the program will continue to execute or else it will terminate from the point where the exception occurs. So its a good practice to handle all the exceptions before they get generated. 

In exception handling we come across five keywords they are. 

try 
catch 
finally 
throw 
throws 

Note: 

See the class notes for more and clear examples. 

Finally : finally is the block which contain the code that need to executed even though the exception occurs( after catch block execution) and also even after execution of try block. 

try 

} catch (Exception e) { 

} finally 

We cant write only try block without a catch block, but we can write a try block with the finally block

Throw v/s Throws in java 

1. Throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly. 

2. If we see syntax wise than throw is followed by an instance variable and throws is followed by exception class names. 

3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).

ALSO SEARCH:

"exception handling in java with examples pdf"

"exception handling in java"

"try catch java"

"user defined exception handling in java"

"exception handling in python"

"types of exception handling in java"

"exception handling in c"

"types of exceptions in java"

"custom exception handling in java with example program"

"types of exception handling in java with example"