SlideShare a Scribd company logo
⦿The Exception Handling in Java isone of
the powerful mechanism to handle the
runtime errors so that normal flow of the
application can be maintained.
⦿we will learn about Java exceptions, its
type and the difference between
checked and unchecked exceptions.
⦿Dictionary Meaning: Exception isan
abnormal condition.
⦿THECORE ADVANTAGE OFEXCEPTION HANDLINGISTO MAINTAIN
THE NORMAL FLOW OF THE APPLICATION.
⦿Suppose there are 10 statements in your
program and there occurs an exception
at statement 5, the rest of the code will
not be executed i.e. statement 6 to 10
will not be executed.
⦿ If we perform exception handling, the
rest of the statement will be executed.
That iswhy we use exception handling
in Java.
⦿Checked Exception
⦿ Unchecked Exception
⦿Error
1) Checked Exception
T
he classes which directly inherit T
hrowable class except
RuntimeException and Error are known as checked exceptions
e.g. IOException, SQLException etc
Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes which inherit RuntimeException are known as
unchecked exceptions
e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they
are checked at runtime.
3) Error
Error isirrecoverable
e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
In the above example, 100/0 raises an ArithmeticException which is
handled by a try-catch block.
⦿ Exception handling allows us to control the
normal flow of the program by using
exception handling in program.
⦿ It throws an exception whenever a calling
method encounters an error providing that
the calling method takes care of that error.
⦿ It also gives us the scope of organizing and
differentiating between different error types
using a separate block of codes. Thisis
done with the help of try-catch blocks
There are given some scenarios where
unchecked exceptions may occur.
They are as follows:
1) A scenario where ArithmeticException
occurs If we divide any number by zero,
there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
2) A scenario where NullPointerException
occurs
Ifwe have a null value in any variable,
performing any operation on the
variable throws a NullPointerException.
String s=
null;
System.out.println(s.length());//NullPointerEx
ception
3) A scenario where NumberFormatException
occurs
The wrong formatting of any value
may occur NumberFormatException.
Suppose Ihave a string variable that has
characters, converting thisvariable into
digit will occur NumberFormatException.
String s="abc";
int i=
Integer.parseInt(s);//NumberFormatExcep
tion
4) A scenario where
ArrayIndexOutOfBoundsException
occurs
If you are inserting any value in the wrong
index, it would result in
ArrayIndexOutOfBoundsException as
shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsExcepti
on
⦿ The JVM firstly checks whether the
exception ishandled or not.
⦿ If exception isnot handled, JVM provides a
default exception handler that performs the
following tasks:
⦿Prints out exception description.
⦿ Prints the stack trace (Hierarchy of
methods where the exception
occurred).
⦿ Causes the program to terminate.
⦿ But if exception ishandled by the
application programmer, normal flow of the
application is maintained i.e. rest of the
code isexecuted.
⦿ This small program includes an expression that
intentionally causes a divide-by-zero error:
classExc0 {
public static void main(String args[])
{int d =0; int a =42 / d; }}
When the Java run-time system detects the
attempt to divide by zero, it constructs a new
exception object and then throws this exception.
Thiscausesthe execution of Exc0 to stop,
because once an exception has been thrown, it must
be caught by an exception handler and dealt with
immediately
⦿ Stack Trace isa list of method calls from the
point when the application was started to the
point where the exception was thrown. The
most recent method callsare at the top.
⦿ A stack trace isa very helpful debugging tool.
It isa list of the method calls that the
application was in the middle of when an
Exception was thrown.
⦿ This isvery useful because it doesn't only show
you where the error happened, but also how
the program ended up in that place of the
code.
⦿ Insome cases, more than one exception
could be raised by a single piece of code.
To handle this type of situation, you can
specify two or more catch clauses, each
catching a different type of exception.
⦿ When an exception isthrown, each catch
statement isinspected in order, and the first
one whose type matches that of the
exception isexecuted. After one catch
statement executes, the othersare
bypassed, and execution continues after
the try/catch block.
⦿ Insome cases, more than one exception
could be raised by a single piece of code.
To handle this type of situation, you can
specify two or more catch clauses, each
catching a different type of exception.
⦿ When an exception isthrown, each catch
statement isinspected in order, and the first
one whose type matches that of the
exception isexecuted. After one catch
statement executes, the othersare
bypassed, and execution continues after
the try/catch block.
Arithmetic Exception occurs
rest of the code
⦿At a time only one exception occurs and
at a time only one catch block is
executed.
⦿All catch blocks must be ordered from
most specific to most general, i.e. catch
for ArithmeticException must come
before catch for Exception
⦿ The try statement can be nested. That is, a try
statement can be inside the block of another try.
Each time a try statement isentered, the context of
that exception ispushed on the stack.
⦿ If an inner try statement does not have a catch
handler for a particular exception, the stack is
unwound and the next try statement’s catch
handlers are inspected for a match.
⦿ Thiscontinuesuntil one of the catch statements
succeeds, or until all of the nested try statements are
exhausted. If no catch statement matches, then the
Java run-time system will handle the exception
⦿Java finally block isa block that is
used to execute important code such as
closing connection, stream etc.
⦿Java finally block is always executed
whether exception ishandled or not.
⦿Java finally block follows try or catch
block.
⦿ For each try block there can be zero or
more catch blocks, but only one finally
block.
⦿The finally block will not be executed if
program exits(either by calling
System.exit() or by causing a fatal error
that causes the process to abort).
⦿ The Java throws keyword isused to declare
an exception. It gives an information to the
programmer that there may occur an
exception so it isbetter for the programmer
to provide the exception handling code so
that normal flow can be maintained.
⦿ Exception Handling is mainly used to handle
the checked exceptions. If there occurs
any unchecked exception such as
NullPointerException, it isprogrammers fault
that he isnot performing check up before
the code being used.
Syntax of java throws
return_type method_name() throws except
ion_class_name{
//method code
}
unchecked Exception: underyour control
so correct your code.
error: beyond your control e.g. you are
unable to do anything if there occurs
VirtualMachineError or
StackOverflowError
⦿NOWCHECKED EXCEPTIONCAN BE PROPAGATED
(FORWARDED INCALLSTACK).
⦿I
TPROVIDESINFORMATIONTOTHECALLEROFTHE METHODABOUT
THE EXCEPTION
Exception handling and throw and throws keyword in java.pptx
Exception handling and throw and throws keyword in java.pptx
Exception handling and throw and throws keyword in java.pptx

More Related Content

Similar to Exception handling and throw and throws keyword in java.pptx

exceptions in java
exceptions in javaexceptions in java
exceptions in java
javeed_mhd
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
poongothai11
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
Nagaraju Pamarthi
 
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
AKSHAYBHABAD5
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Ankit Rai
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
DrHemlathadhevi
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
Manav Prasad
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
EduclentMegasoftel
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docx
Poornima E.G.
 
exception handling
exception handlingexception handling
exception handling
Manav Dharman
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Java chapter 6
Java chapter 6Java chapter 6
Java chapter 6
Abdii Rashid
 
Exception handling
Exception handlingException handling
Exception handling
Tata Consultancy Services
 
Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
Swabhav Techlabs
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
 

Similar to Exception handling and throw and throws keyword in java.pptx (20)

exceptions in java
exceptions in javaexceptions in java
exceptions in java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025unit 4 msbte syallbus for sem 4 2024-2025
unit 4 msbte syallbus for sem 4 2024-2025
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docx
 
exception handling
exception handlingexception handling
exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Java chapter 6
Java chapter 6Java chapter 6
Java chapter 6
 
Exception handling
Exception handlingException handling
Exception handling
 
Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 

Recently uploaded

Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
Indrajeet sahu
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
Shiny Christobel
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
AnasAhmadNoor
 
2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt
abdatawakjira
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 

Recently uploaded (20)

Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
 
2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 

Exception handling and throw and throws keyword in java.pptx

  • 1.
  • 2. ⦿The Exception Handling in Java isone of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained. ⦿we will learn about Java exceptions, its type and the difference between checked and unchecked exceptions. ⦿Dictionary Meaning: Exception isan abnormal condition.
  • 3. ⦿THECORE ADVANTAGE OFEXCEPTION HANDLINGISTO MAINTAIN THE NORMAL FLOW OF THE APPLICATION.
  • 4. ⦿Suppose there are 10 statements in your program and there occurs an exception at statement 5, the rest of the code will not be executed i.e. statement 6 to 10 will not be executed. ⦿ If we perform exception handling, the rest of the statement will be executed. That iswhy we use exception handling in Java.
  • 5.
  • 6.
  • 8. 1) Checked Exception T he classes which directly inherit T hrowable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc Checked exceptions are checked at compile-time. 2) Unchecked Exception The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime. 3) Error Error isirrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
  • 9.
  • 10. In the above example, 100/0 raises an ArithmeticException which is handled by a try-catch block.
  • 11. ⦿ Exception handling allows us to control the normal flow of the program by using exception handling in program. ⦿ It throws an exception whenever a calling method encounters an error providing that the calling method takes care of that error. ⦿ It also gives us the scope of organizing and differentiating between different error types using a separate block of codes. Thisis done with the help of try-catch blocks
  • 12. There are given some scenarios where unchecked exceptions may occur. They are as follows: 1) A scenario where ArithmeticException occurs If we divide any number by zero, there occurs an ArithmeticException. int a=50/0;//ArithmeticException
  • 13. 2) A scenario where NullPointerException occurs Ifwe have a null value in any variable, performing any operation on the variable throws a NullPointerException. String s= null; System.out.println(s.length());//NullPointerEx ception
  • 14. 3) A scenario where NumberFormatException occurs The wrong formatting of any value may occur NumberFormatException. Suppose Ihave a string variable that has characters, converting thisvariable into digit will occur NumberFormatException. String s="abc"; int i= Integer.parseInt(s);//NumberFormatExcep tion
  • 15. 4) A scenario where ArrayIndexOutOfBoundsException occurs If you are inserting any value in the wrong index, it would result in ArrayIndexOutOfBoundsException as shown below: int a[]=new int[5]; a[10]=50; //ArrayIndexOutOfBoundsExcepti on
  • 16.
  • 17.
  • 18.
  • 19. ⦿ The JVM firstly checks whether the exception ishandled or not. ⦿ If exception isnot handled, JVM provides a default exception handler that performs the following tasks: ⦿Prints out exception description. ⦿ Prints the stack trace (Hierarchy of methods where the exception occurred). ⦿ Causes the program to terminate. ⦿ But if exception ishandled by the application programmer, normal flow of the application is maintained i.e. rest of the code isexecuted.
  • 20.
  • 21. ⦿ This small program includes an expression that intentionally causes a divide-by-zero error: classExc0 { public static void main(String args[]) {int d =0; int a =42 / d; }} When the Java run-time system detects the attempt to divide by zero, it constructs a new exception object and then throws this exception. Thiscausesthe execution of Exc0 to stop, because once an exception has been thrown, it must be caught by an exception handler and dealt with immediately
  • 22. ⦿ Stack Trace isa list of method calls from the point when the application was started to the point where the exception was thrown. The most recent method callsare at the top. ⦿ A stack trace isa very helpful debugging tool. It isa list of the method calls that the application was in the middle of when an Exception was thrown. ⦿ This isvery useful because it doesn't only show you where the error happened, but also how the program ended up in that place of the code.
  • 23. ⦿ Insome cases, more than one exception could be raised by a single piece of code. To handle this type of situation, you can specify two or more catch clauses, each catching a different type of exception. ⦿ When an exception isthrown, each catch statement isinspected in order, and the first one whose type matches that of the exception isexecuted. After one catch statement executes, the othersare bypassed, and execution continues after the try/catch block.
  • 24. ⦿ Insome cases, more than one exception could be raised by a single piece of code. To handle this type of situation, you can specify two or more catch clauses, each catching a different type of exception. ⦿ When an exception isthrown, each catch statement isinspected in order, and the first one whose type matches that of the exception isexecuted. After one catch statement executes, the othersare bypassed, and execution continues after the try/catch block.
  • 26. ⦿At a time only one exception occurs and at a time only one catch block is executed. ⦿All catch blocks must be ordered from most specific to most general, i.e. catch for ArithmeticException must come before catch for Exception
  • 27. ⦿ The try statement can be nested. That is, a try statement can be inside the block of another try. Each time a try statement isentered, the context of that exception ispushed on the stack. ⦿ If an inner try statement does not have a catch handler for a particular exception, the stack is unwound and the next try statement’s catch handlers are inspected for a match. ⦿ Thiscontinuesuntil one of the catch statements succeeds, or until all of the nested try statements are exhausted. If no catch statement matches, then the Java run-time system will handle the exception
  • 28.
  • 29. ⦿Java finally block isa block that is used to execute important code such as closing connection, stream etc. ⦿Java finally block is always executed whether exception ishandled or not. ⦿Java finally block follows try or catch block.
  • 30. ⦿ For each try block there can be zero or more catch blocks, but only one finally block. ⦿The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
  • 31.
  • 32. ⦿ The Java throws keyword isused to declare an exception. It gives an information to the programmer that there may occur an exception so it isbetter for the programmer to provide the exception handling code so that normal flow can be maintained. ⦿ Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it isprogrammers fault that he isnot performing check up before the code being used.
  • 33. Syntax of java throws return_type method_name() throws except ion_class_name{ //method code } unchecked Exception: underyour control so correct your code. error: beyond your control e.g. you are unable to do anything if there occurs VirtualMachineError or StackOverflowError
  • 34. ⦿NOWCHECKED EXCEPTIONCAN BE PROPAGATED (FORWARDED INCALLSTACK). ⦿I TPROVIDESINFORMATIONTOTHECALLEROFTHE METHODABOUT THE EXCEPTION