0% found this document useful (0 votes)
11 views

Unit-03 note-04 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit-03 note-04 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

finally statement:-

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[])
{

Scanner ob=new Scanner(System.in);


int a;
try
{
System.out.println("Enter the divisor:");

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:

Enter the divisor: 0


Division by zero not possible
Enter the divisor: abc
Please enter integer value

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
{

ArithmeticException ae=new ArithmeticException();


throw ae; // or throw new ArithmeticException();
}
catch(ArithmeticException ae)

{
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
{

public static void main(String args[])


{
try
{

ArithmeticException ae=new ArithmeticException("division by zero error");


ae.initCause(new IOException("input error"));
throw ae;
}
catch(ArithmeticException ae)

{
System.out.println("Caught:"+ae);
System.out.println("Actual cause:"+ ae.getCause());
}

}
}
Output
Caught: java.lang.ArithmeticException: division by zero error

Actual cause: java.io.IOException: input error

Built-in Exception Classes:


Built-in exception classes are exceptions which are available in Java libraries. The
exception classes are,
1. ArithmeticException: It is thrown when an exceptional condition has occurred in an
arithmetic operation. Eg: Division by zero error
2. ArrayIndexOutOfBoundsException: It is thrown an array has been accessed using an
illegal index.
3. InputMismatchException: It is thrown when the user does not provide the proper
type of input.
4. StringIndexOutOfBoundsException: It is thrown a character of a string has been
accessed using an illegal index.
5. NumberFormatException: It is thrown when an attempt is made to convert a string
with improper format into a numeric value.
6. IOException: It is thrown when an input-output operation failed or interrupted.
7. SQLException: It occurs if there is an error in the database access.
8. InterruptedException: It is thrown when a thread is waiting, sleeping, or doing some
processing, and it is interrupted.
9. ClassNotFoundException: It is thrown when we try to access a class whose definition
is not found.
10. FileNotFoundException: It is thrown when a file is not accessible or does not open.
Checked and unchecked exceptions:
Checked exceptions are the exceptions that are checked at compile-time. If some
code within a method throws a checked exception, then the method must either handle the
exception or it must specify the exception using throws keyword. Otherwise, the compiler
generates an error message.

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

You might also like