0 ratings0% found this document useful (0 votes) 17 views17 pagesJava Topics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES
TIRUPATKAUTONOMOUS)
Emerging Technology(AIML)
A.Y#2023-2024
Seminar on
EXCEPTION HANDLING
Seminar By:
Name + LKAVYA
Head Of the Department
Dr. C. Siva Balaji
Re Under the Guidance of
Sec:O1
Mr. D NAGA BHUSHANAM GARU, M, Te
Department of Al
ATS, Tirupati
Assistant Professor
Department of CSEContents
\\ S85 8 8 See
ra
What is an Exception ?
terme es te
Hierarchy of Java Exception classes
Java Exception Handling Keywords
oR e Ses
Internal working of java try-catch block
ised eee
Java Multi Catch block
The Throws / Throw Keywords
The finally block
Common scenarios where exceptions may occur
RY eee
(alee ses
ee hse Ld
User-defined Exceptions:What is an exception?
ee SEE ues
ENE Rue eeu UC Rhee Ue ce
Cy SUR CE Seo
ee ae uml lr)
Pate anu re Me Ue cee ee Reo elie a
10, SQL, Remote etc.
U Advantage of Exception Handling
Beene eee CU eee RUC CURR Riek mlm
Precise CUE A eR ALOR ure]
that is why we use exception handling.Realtime Example of Exception in Java
1. Suppose you are watching a video on Youtube, suddenly, internet
connectivity is disconnected or not working. In this case, you are
not able to continue watching the video on Youtube. This
interruption is nothing but an exception.
-@->
8
a
Exception
A git is watching a video on Interrupted in watching
‘Youtube on the computer video due to internet
disconnectivity suddenly
Stopped
————P ca > Puncture repaired
ee Exception Handled
Exception
Fig: Realtime Example of Exception Handlinglt sag
THROWABLE
ERRORS EXCEPTIONS
CHECKED EXCEPTION UNCHECKED EXCEPTIError vs Exception
’ Errors: An error represents a
OU COM SECT MeO MALT Le
team rene tle) carver
SIUM oe am CoRe Te
Virtual Machine Error
MO A
~ Stack overflow
Mee Mee ee)
Cae ai Crea atta
ee ol MAU Stee
SUN anmrce ene
applications should
(rat
Array index out of bounds
Arithmetic errors (divide by
zero
Null Pointer Exception
W/O ExceptionsInternal working of java try-catch block |
An object of exception class is thrown
int data = 10/0; fi eer Exception
object
y
ai)
NO
Rest of code
is executedJava Multi Catch block
public class TestMultipleCatchBlock{
public static void main(String args{}){
Ze
int a[J=new int[5);
Exo
}
{[Link]("task1 is completed");}
{[Link]("task 2 completed");}
{[Link]("common task completed");}
[Link]("rest of the code...");
}
}
ata time only one Exception is occurred and at a time only one catch
te ae av
OAll catch blocks must be ordered from most specific to most general i.e.
eee me aaa ea ee ek ee eethrow keyword
> throw :
© Used to explicitly throw an exception
@ Can be used to throw checked, unchecked and custom exception
tlic clans Testthrow
Output:
stan woud abate it age
ages) [Ceception in tomad mn java ang AnthmencEaception nat vad
rrom nem AnthenetEacaption( net vai")
‘System out printin( welcome to vote")
)
uh sat vod main(String arya].
wade)
‘System out printing vest ofthe code")
>
>throws keyword
—
> throws :
@ Used to declare an exception
@ Gives an 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.
lmport java io AOE xception;
aes Vesttnrows{
void seconsMetnot() throws IOExCEStON {
vom nem 10Exception("dewce error").// Checked exception Cxtoet
> svcepton need
oid fraebethoat) throws 1OExcegtION { ermal tow
seconditethod)
>
Public static void main final String angst) {
{hnal Testthrows cb = new Testtrvoms();
we
by firsts);
> catch (final Excaption &) ¢
System ou pritin( “exception handled");
>
‘System out printin(“ncema flow."
>finally block
> Follows a try block.
> Always executes, whether or not an exception has occurred.
Allows you to run any cleanup-type statements that you want to execute, no
matter what happens in the protected code.
> Executes right before the return executes present in try block,
‘Syntax of try-finally block:
teyt
// Protected Code that may throw exception
Keatch(Exception ex)(
// Catch block may or may not execute
Hinaty{
1/ The finally block always executes.Common scenarios where exceptions may occur |
Ser lile =bCell Loli melee ey
int a=50/0 /ArithmeticException
Belin claas cell lel Melero ed
String s=null
[Link]([Link]()) /NullPointerException
* NumberFormatException occurs
String s="abc"
int i=[Link](s) NumberFormatException
* ArraylndexOutOfBoundsException occurs
=new int[5]Types of Exceptions
— Checked Exceptions
@ Checked at compile time
Must be either handled or
specified using throws keyword
> Unchecked Exceptions
Not checked at compile time
@ Also called as Runtime ExceptionsChecked Exception Example h®
> import [Link].*;
class Main {
public static void main(Stringl} args) {
FileReader file = new FileReader("[Link]");
BufferedReader fileinput = new BufferedReader(file);
q
}
> Compilation Error:
Exception in thread “main” [Link]: Uncompilable source code
unreported exception [Link]; must be caught or declored to be
thrown
at [Link](Mainjava:5)Runtime Exception Example B®
> class Main {
public static void main(String args[]) {
int
int y = 10;
int 2 = y/x;
}
}
> Exception in thread ~— "main" —_java.[Link]: / by —_ zero
at [Link](Main,java:5)
Java Result: 1User-defined Exceptions: |
Axe etc el me ele BE my Rea Rel lO rir 4
points in mind when writing your own exception classes.
PN Ro ese ieee Rell Rem ice ele) (om
DRM a OR Ce Rearecem emul ace ice lee] ig
enforced by the Handle or Declare Rule, you need to extend the
Latest ma Con
DAR CURR Ce Meo elm el Rue R ck c ue R Cl)
Tes ce eee oe
We can define our own Exception class as below:
Eis ee tues eo weTHANK YOU
HAVE A NICE DAY!