Unit-03 note-04 (1)
Unit-03 note-04 (1)
Finally block in Java can be used to put clean-up code such as closing a file, closing database
connection, etc. Java finally block is always executed whether the exception is handled or not.
Java finally block follows try or catch block.
Syntax:
try try
{ {
//statements //statements
} }
catch(Exception_type e) finally
{ {
// statements // final code
} }
finally
{
// final code
}
Eg:- try
{
System.out.println(5/0);
}
catch(ArithmeticException ae)
{
System.out.println(“Division by zero not possible…”);
}
finally
{ Output:
System.out.println(“I am always executed”); Division by zero not possible…
} I am always executed
Nested try
We can use a try statement within another try statement. This structure is
called nested-try.
Syntax:
try
{
//statement
try
{
// statement
}
catch(Exception_type e)
{
// statement
}
}
catch(Exception_type e)
{
// statement
}
Example:
import java.util.*;
class Nested
{
public static void main(String args[])
{
a=ob.nextInt ();
try
{
System.out.println(10/a);
}
catch(ArithmeticException e)
{
System.out.println(“Division by zero not possible”);
}
}
catch(InputMismatchException e)
{
System.out.println(“Please enter integer value”);
}
}
}
Output:
throws keyword:
The throws keyword delegates the responsibility of the exception handling to the caller
of the method. We can do this by including a throws keyword in the method’s declaration.
Syntax:
type methodname(paramaters) throws exception_list
{
// method body
}
Where, exception_list is a comma-separated list of the exceptions that a method can throw.
Example:
class Demo
{
static void f1() throws ArithmeticException
{
System.out.println(5/0);
}
public static void main(String args[])
{
try
{
f1();
}
catch(ArithmeticException e)
{
System.out.println(“Division by zero not possible”);
}
}
throw keyword:
The throw keyword in Java is used to explicitly throw an exception from a method or any
block of code. The throw keyword is mainly used to throw custom or user-defined exceptions.
Syntax: throw Exception_object;
Example:
try
{
{
System.out.println(“Division by zero not possible”);
}
User-defined Exception:
We can create our own exception class to handle the user-defined exception. We can
do this by extending any built-in exception class like Exception and using throw keyword.
Example:
import java.util.*;
class InvalidRollnoException extends Exception
{
}
class Demo
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
int rno;
System.out.println("Enter the rollno:");
rno=ob.nextInt ();
try
{
if(rno<1)
{
throw new InvalidRollnoException();
}
System.out.println(rno);
}
catch(InvalidRollnoException e)
{
System.out.println(“Rollno is invalid”);
} Output:
} Enter the rollno: -5
} Rollno is invalid
Chained Exception:
Chained exception helps to identify a situation in which one exception causes another
exception in an application. For example, consider a method which throws an
ArithmeticException because of an attempt to divide by zero. But, the actual cause of
exception was an I/O error which is caused by the divisor to be zero. The method will throw
the ArithmeticException to the caller. The caller would not know about the actual cause of an
exception. Chained exception is used in such situations. Java exception class provides two
methods to handle chained exception.
1. initCause():- It initializes or sets the actual cause of an exception
2. getCause():- It returns the actual cause associated with current exception
Example:
import java.io.*;
class Chained
{
{
System.out.println("Caught:"+ae);
System.out.println("Actual cause:"+ ae.getCause());
}
}
}
Output
Caught: java.lang.ArithmeticException: division by zero error
Example:
1. IOException
2. SQLException
3. InterruptedException
4. ClassNotFoundException
5. FileNotFoundException
Unchecked exceptions are the exceptions that are not checked at compile-time. So,
it is not forced by the compiler to either handle or specify the exception.
Example:
1. ArithmeticException
2. NumberFormatException
3. InputMismatchException
4. ArrayIndexOutOfBoundsException
5. StringIndexOutOfBoundsException