0% found this document useful (0 votes)
303 views18 pages

Exception Handling in Java

This document discusses exception handling in Java. It defines exceptions as abnormal or unexpected conditions that disrupt normal program flow. Exception handling allows programs to gracefully handle errors at runtime rather than terminating abruptly. The document covers checked and unchecked exceptions, try-catch blocks, finally blocks, and the throw and throws keywords used in exception handling. It provides examples of programs with and without exception handling to demonstrate how exception handling maintains normal program flow even when exceptions occur.

Uploaded by

ajay patel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
303 views18 pages

Exception Handling in Java

This document discusses exception handling in Java. It defines exceptions as abnormal or unexpected conditions that disrupt normal program flow. Exception handling allows programs to gracefully handle errors at runtime rather than terminating abruptly. The document covers checked and unchecked exceptions, try-catch blocks, finally blocks, and the throw and throws keywords used in exception handling. It provides examples of programs with and without exception handling to demonstrate how exception handling maintains normal program flow even when exceptions occur.

Uploaded by

ajay patel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 18

EXCEPTION HANDLING

IN JAVA

PR E SENTED BY PR E SENTED TO
NA M E : - A J AY PAT EL E R . M OHIT PAU L
: - A NUJ BHA I PAT EL A S S ISTANT PROF E SSOR
CL A S S : BS C. CS D E PARTM ENT OF CS
ID. NO: - 15 B SC SC04 1
ID. NO: - 15 B S C SC044
S U BJ ECT COD E : - CS IT 423
Define :-Exception
Meaning: Exception is an abnormal condition or unexpected
condition.

In java exception is an unexpected or unwanted problem that


disturb normal flow of program .

When an Exception occurs the normal flow of the program is disrupted


and the program terminates abnormally, which is not recommended
therefore ,these exceptions are to be handled.
Define :-Exception Handling
Runtime error
Exception Handling is a mechanism to handle runtime errors .

An exception can occur for many different reasons.


A user has entered an invalid data.
Divide by zero errors
Opening a non-existent file
A network connection has been lost in the middle of communication
in program.
Types of Exception
we need to understand them to know how exception handling
works in Java.
There are mainly three type of exception
Checked Exception
Unchecked Exception
Error( error is considered as unchecked exception)
Checked exceptions
A checked exception is an exception that occurs at the compile time.
Checked are also called as compile time exceptions.
Unchecked Exception
An unchecked exception is an exception that occurs at the time of
execution .
Unchecked are also called as Runtime Exceptions.
The classes that extent Runtime Exception are known as unchecked
exception.
Error: error is irrecoverable .
Example:-out of memory error.
Java Exception handling managed by using five
keywords.
try
catch
finally
throw
throws
Try:- In try block we have to write the statement that causes exception.

Catch:- catch block can handle the exception and handle it.
Program Without exception handling
Program Out put
without exception handling
Program by Exception handling
OUT PUT
Program with exception handling
Finally block
Java finally block is a block that is used to execute
important code such as closing connection,
stream etc.
Java finally block is always executed whether
exception is handled or not.
Java finally block must be followed by try and
catch block .
Java finally Program
where Exception occurs and handled
Throw keyword
Throw keyword is used to explicitly throw an exception.
The throw keyword is mainly used to throw custom
exception
We can throw either checked or unchecked exception in
java by throw keyword.
Syntax of java throw keyword program

C:\Java>java excep 15
exception in thread main java .lang. Arithmetic Exception: not valid to give vote
Throws Keyword

Throws keyword is used to declare an exception.


It provide Information to the programmer that there may
occur an exception
so it is better for the programmer to provide the exception
handling code so that normal flow can be maintained.
Class M
{
Void method() throws IO Exception
{
System. out. println(device operation performed);
}
}
Class Testthrows3
{
Public static void main(String args[])throw IO Exception//declare
Exception
M m= new M();
m.method(); Output device operation
System. out. println(normalo flow);
} performed normal flow
}
THANK YOU

You might also like