0% found this document useful (0 votes)
86 views11 pages

Exception Handling

Exception handling in Java allows the normal flow of a program to continue despite runtime errors. Exceptions can be checked, unchecked, or errors. Checked exceptions must be declared or caught, while unchecked exceptions and errors are not checked at compile time. The try block specifies code that might throw exceptions, while catch blocks handle specific exceptions. Finally blocks contain cleanup code that always executes. Throws and throw are used to explicitly throw exceptions from methods or when conditions occur. Custom exceptions can be created by extending the Exception class.

Uploaded by

Vani Mittal
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
86 views11 pages

Exception Handling

Exception handling in Java allows the normal flow of a program to continue despite runtime errors. Exceptions can be checked, unchecked, or errors. Checked exceptions must be declared or caught, while unchecked exceptions and errors are not checked at compile time. The try block specifies code that might throw exceptions, while catch blocks handle specific exceptions. Finally blocks contain cleanup code that always executes. Throws and throw are used to explicitly throw exceptions from methods or when conditions occur. Custom exceptions can be created by extending the Exception class.

Uploaded by

Vani Mittal
Copyright
© © All Rights Reserved
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/ 11

Exception Handling

Definition –
Exception Handling in Java is one of the powerful mechanisms to handle the
runtime errors so that the normal flow of the application can be maintained.
Exception Handling is a mechanism to handle runtime errors.
statement 1;  
statement 2;  
statement 3;  
statement 4;  
statement 5;//exception occurs  
statement 6;  
statement 7;  
statement 8;  
Suppose there are 8 statements in a Java program and an exception occurs at
statement 5; the rest of the code will not be executed, i.e., statements 6 to 8 will not
be executed.
Hierarchy of Java Exception classes
Types of Java
Exceptions
Checked Exception
The classes that directly inherit the Throwable class except RuntimeException and Error
are known as checked exceptions. For example, IOException, SQLException, etc. Checked
exceptions are checked at compile-time.

Unchecked Exception
The classes that inherit the RuntimeException are known as unchecked exceptions.
For example, ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException, etc. Unchecked exceptions are not checked at
compile-time, but they are checked at runtime.

Error
Error is irrecoverable.
Some example of errors are OutOfMemoryError, VirtualMachineError, AssertionError etc.
Types of Java
Exceptions
Checked Exception
The classes that directly inherit the Throwable class except RuntimeException and Error
are known as checked exceptions. For example, IOException, SQLException, etc. Checked
exceptions are checked at compile-time.

Java Exception Keywords

Keyword Description
try The "try" keyword is used to specify a block where we should place an
exception code. It means we can't use try block alone. The try block must be
followed by either catch or finally.
catch The "catch" block is used to handle the exception. It must be preceded by try
block which means we can't use catch block alone. It can be followed by
finally block later.
public class JavaExceptionExample{  
  public static void main(String args[]){   An arithmetic exception object is created.
   try{  
An arithmetic exception object is thrown
       automatically.
       int data=100/0;  
    } Exception object caught by the catch
catch(ArithmeticException e) block.
{ Catch block prints the error message.
System.out.println(e);
}  
   
System.out.println("rest of the code...");  
  }  
}  
Single try multiple catch block

public class JavaExceptionExample{  
  public static void main(String args[]){  
   try{  
        int x = Integer.parseInt(args[0]);
5. int data=100/x;
Comment –
int arr[] = {2,3,4};
7. System.out.println(arr[x]);   If the value of x = 0. Then block-1 is
    } catch(ArithmeticException e) executed. And if the value of x is
{ System.out.println(e); 1 greater than 2 then block-2 is
}   executed.
    catch(ArrayIndexOutOfBoundsException ae)
{ System.out.println(ae); 2
}
System.out.println("rest of the code...");  
  }  
}  
public class JavaExceptionExample{  
  public static void main(String args[]){  
   try{   Comment –
        int x = Integer.parseInt(args[0]);
5. int data=100/x; This code handles all types of
int arr[] = {2,3,4}; exceptions. As we use the Exception
7. System.out.println(arr[x]);   superclass which inherits all
    } catch(Exception e) subclasses of exception.
{ System.out.println(e);
}  
    System.out.println("rest of the code...");  
  }  
}  
throws and throw
If a function not wants to handle exception in its
public class JavaExceptionExample{   functional body. Then we can use throws clause to
throw the generated exception from the function.
static void calculate() throws ArithmeticException,

ArrayIndexOutOfBoundsException
{
int x = Integer.parseInt(args[0]);
int data=100/x;
int arr[] = {2,3,4}; The generated exception thrown from the function
System.out.println(arr[x]);   will be caught in the try-catch block from where the
} function calling taking place.
  public static void main(String args[]){  
   try{
calculate();  
            } catch(ArithmeticException ae)
{ System.out.println(e); }
catch(ArrayIndexOutOfBoundsException are)
{ System.out.println(are); }   
    System.out.println("rest of the code...");  
  }  
}  
throw clause
public class JavaExceptionExample{  
If you want to explicitly throw an exception on certain condition
in a program we use throw clause. static void calculate() {
int x = Integer.parseInt(args[0]);
if(x = = 0)
throw new NullPointerException();
A NullPointerException will be explicitly thrown if the
}
value of x = 0.
  public static void main(String args[]){  
   try{
calculate();  
            } catch(NullPointerException ne)
{ System.out.println(ne); }
  
    System.out.println("rest of the code...");  
  }  
}  
User defined exception
Steps for defined a user exception –
1. Create a user exception class for example – Extends Exception
“OwnException”. superclass
2. Extends the class with Exception superclass.
class OwnException extends Exception
3. Explicitly throw the user exception from where it is {
generated in a function. String errormsg = "Value greater than 10 not accepted";
4. Handle the exception from the try-catch block from public String toString()
where the function is invoked which generates the { return errormsg;
}
exception }
public class UserDefinedException {
static void input(int x) throws OwnException
{
if(x > 10)
Note – throw the object of OwnException. throw new OwnException();
else
System.out.println("Value Accepted...");
When we print OwnException }
object “oe” in System.out. public static void main(String args[])
{ try
println() it automatically call
{
toString() method of OwnException object caught here int v = Integer.parseInt(args[0]);
OwnException class. in input(v);
the catch block. } catch(OwnException1 oe)
{System.out.println(oe);
}
}}

You might also like