Exception Handling in Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18
At a glance
Powered by AI
The key takeaways from the document are that exceptions in Java are unexpected problems that disrupt normal program flow and need to be handled. There are different types of exceptions like checked exceptions, unchecked exceptions and errors. Exception handling in Java uses keywords like try, catch, throw and throws to handle exceptions.

The different types of exceptions in Java are checked exceptions, unchecked exceptions and errors. Checked exceptions occur at compile time while unchecked exceptions occur at runtime. Errors are irrecoverable exceptions like OutOfMemoryError.

The different keywords used in exception handling in Java are try, catch, finally, throw and throws. The try block contains code that might throw an exception. The catch block handles the exception. The finally block always executes to cleanup resources. The throw keyword explicitly throws an exception while throws declares which exceptions a method can throw.

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