0% found this document useful (0 votes)
5 views10 pages

Lecture - 9 (Java) E-Notes

The document discusses exception handling in Java, explaining that exceptions are events that disrupt the normal flow of execution and must be handled to ensure the application continues running. It categorizes exceptions into checked, unchecked, and errors, detailing how each type is managed using try-catch blocks or the throws keyword. Additionally, it provides an example application demonstrating exception handling with user input and arithmetic operations.

Uploaded by

samanraj823004
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)
5 views10 pages

Lecture - 9 (Java) E-Notes

The document discusses exception handling in Java, explaining that exceptions are events that disrupt the normal flow of execution and must be handled to ensure the application continues running. It categorizes exceptions into checked, unchecked, and errors, detailing how each type is managed using try-catch blocks or the throws keyword. Additionally, it provides an example application demonstrating exception handling with user input and arithmetic operations.

Uploaded by

samanraj823004
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

Training On Java

Lecture – 9
Exception Handling In Java
Exception In Java

Information regarding Exception:-


 Dictionary meaning of the exception is abnormal termination.
 An expected event that disturbs or terminates normal flow of execution called exception.
 If the application contains exception then the program terminated abnormally the rest of the application is
not executed.
 To overcome above limitation in order to execute the rest of the application must handle the exception.
In java we are having two approaches to handle the exceptions.
1) By using try-catch block.
2) By using throws keyword.
Exception Handling In Java
 The main objective of exception handling is to get normal termination of the application in order to execute
rest of the application code.
 Exception handling means just we are providing alternate code to continue the execution of remaining
code and to get normal termination of the application.
 Every Exception is a predefined class present in different packages.

E.g.
[Link]
[Link]
[Link]
[Link]

The exception are occurred due to two reasons :-

a. Developer mistakes
b. End-user mistakes.
Types Of Exceptions

As per the sun micro systems standards The Exceptions are divided into three types:-

1) Checked Exception

2) Unchecked Exception

3) Error
Checked Exceptions

 The Exceptions which are checked by the compiler at the time of compilation is called Checked
Exceptions. IOException, SQLException, InterruptedException……..etc.
 If the application contains checked exception the code is not compiled so must handle the checked
Exception in two ways
a) By using try-catch block.
b) By using throws keyword.
 If the application contains checked Exception the compiler is able to check it and it will give intimation
to developer regarding Exception in the form of compilation error.
Unchecked Exceptions

 The exceptions which are not checked by the compiler at the time of compilation are called unchecked
Exception. ArithmeticException, ArrayIndexOutOfBoundsException, NumberFormatException…etc.
 If the application contains un-checked Exception code is compiled but at runtime JVM (Default
Exception handler) display exception message then program terminated abnormally.
 To overcome runtime problem must handle the exception in two ways.
a) By using try-catch blocks.
b) By using throws keyword.
Errors

 Errors are caused due to lack of system resources like, Heap memory full, Stack memory problem, AWT
component problems…..etc Ex: - StackOverFlowError, OutOfMemoryError, AssertionError……etc
 Exceptions are caused due to developers mistakes or end user supplied inputs but errors are caused due to
lack of system resources.
 We are handle the exceptions by using try-catch blocks or throws keyword but we are Unable to handle
the errors.
Exception Tree Structure
Exception Handling Syntax

try
{
//Code which you want to protect
}
catch(ExceptionClass obj)
{
//Code which is used to handle exception
}
finally
{
//Code which you want to execute always
}
Example Application
import [Link].*;
class ExDemo {
public static void main(String [] args) {
int x,y,z;
Scanner sc=new Scanner([Link]);
try {
[Link](“Enter two numbers”);
x=[Link]();
y=[Link]();
z=x/y;
[Link](“Result=”+z);
}
catch(InputMismatchException ex1) {
[Link](“Enter numbers only”);
}
catch(ArithmeticException ex2) {
[Link](“Are you trying to / by zero?”);
}}}

You might also like