SlideShare a Scribd company logo
Exception Handling
in Java
MADE BY :
POOJA ROY
1
Exception Handling in Java
 The exception handling in java is one of the powerful mechanism to
handle the runtime errors so that normal flow of the application can be
maintained.
 In this page, we will learn about java exception, its type and the difference
between checked and unchecked exceptions.
2
What is exception
 Dictionary Meaning: Exception is an abnormal condition.
 In java, exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
 Exception Handling is a mechanism to handle runtime errors such as
ClassNotFound, IO, SQL, Remote etc.
3
Advantage of Exception Handling
 The core advantage of exception handling is to maintain the normal flow of the application. Exception normally
disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:
 statement 1;
 statement 2;
 statement 3;
 statement 4;
 statement 5;//exception occurs
 statement 6;
 statement 7;
 statement 8;
 statement 9;
 statement 10;
 Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code
will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement
will be executed. That is why we use exception handling in java.
4
Hierarchy of Java Exception classes 5
Types of Exception
 here are mainly two types of exceptions: checked and unchecked where
error is considered as unchecked exception. The sun microsystem says
there are three types of exceptions:
 Checked Exception
 Unchecked Exception
 Error
6
Difference between checked and
unchecked exceptions
 ) Checked Exception
 The classes that extend Throwable 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 that extend RuntimeException are known as unchecked
exceptions e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not
checked at compile-time rather they are checked at runtime.
 3) Error
 Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError,
AssertionError etc.
7
Common scenarios where exceptions
may occur
 Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
 int a=50/0;//ArithmeticException
Scenario where NullPointerException occurs
 If we have null value in any variable, performing any operation by the
variable occurs an NullPointerException.
 String s=null;
 System.out.println(s.length());//NullPointerException
8
Common scenarios where exceptions
may occur
 Scenario where NumberFormatException occurs
 The wrong formatting of any value, may occur NumberFormatException.
Suppose I have a string variable that have characters, converting this
variable into digit will occur NumberFormatException.
 String s="abc";
 Scenario where ArrayIndexOutOfBoundsException occurs
 If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
 int a[]=new int[5];
 a[10]=50; //ArrayIndexOutOfBoundsException
9
Java Exception Handling Keywords
 There are 5 keywords used in java exception handling.
 try
 catch
 finally
 throw
 throws
10
Java try block
 Java try block is used to enclose the code that might throw an exception. It
must be used within the method.
 Java try block must be followed by either catch or finally block.
 Syntax of java try-catch
 try{
 //code that may throw exception
 }catch(Exception_class_Name ref){}
 Syntax of try-finally block
 try{
 //code that may throw exception
 }finally{}
11
Java catch block
 Java catch block is used to handle the Exception. It must be used after the try
block only.
 You can use multiple catch block with a single try.
 public class Testtrycatch2{
 public static void main(String args[]){
 try{
 int data=50/0;
 }catch(ArithmeticException e){System.out.println(e);}
 System.out.println("rest of the code...");
 }
 }
12
output
Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...
13
Internal working of java try-catch
block
14
Java 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 follows try or catch block.
15
Java finally block 16
Java throw keyword
 The Java throw keyword is used to explicitly throw an exception.
 We can throw either checked or uncheked exception in java by throw
keyword. The throw keyword is mainly used to throw custom exception.
We will see custom exceptions later.
 The syntax of java throw keyword is given below.
17
java throw keyword example
 public class TestThrow1{
 static void validate(int age){
 if(age<18)
 throw new ArithmeticException("not valid");
 else
 System.out.println("welcome to vote");
 }
 public static void main(String args[]){
 validate(13);
 System.out.println("rest of the code...");
 }
 }
18
Java Exception propagation
 class TestExceptionPropagation1{
 void m(){
 int data=50/0;
 }
 void n(){
 m();
 }
 void p(){
 try{
 n();
 }catch(Exception e){System.out.println("exception handled");}
 }

19
Java Exception propagation
 public static void main(String args[]){
 TestExceptionPropagation1 obj=new TestExceptionPropagation1();
 obj.p();
 System.out.println("normal flow...");
 }
 }
20
Java Exception propagation 21
Java Exception propagation
 In the above example exception occurs in m() method where it is not
handled,so it is propagated to previous n() method where it is not
handled, again it is propagated to p() method where exception is
handled.
 Exception can be handled in any method in call stack either in main()
method,p() method,n() method or m() method.
22
Java throws keyword
 The Java throws keyword is used to declare an exception. It 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.
 Exception Handling is mainly used to handle the checked exceptions. If
there occurs any unchecked exception such as NullPointerException, it is
programmers fault that he is not performing check up before the code
being used.
23
Difference between throw and throws
in Java
throw
 Java throw keyword is used to
explicitly throw an exception.
 Checked exception cannot be
propagated using throw only.
 Throw is followed by an instance.
 Throw is used within the method.
 You cannot throw multiple
exceptions.
throws
 Java throws keyword is used to
declare an exception.
 Checked exception can be
propagated with throws.
 Throws is followed by class.
 Throws is used with the method
signature.
 You can declare multiple exceptions
e.g.
public void method()throws
IOException,SQLException.
24
Thank you
25
Ad

More Related Content

What's hot (20)

Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Pratik Soares
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
aptechsravan
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
Deepak Sharma
 
exception handling
exception handlingexception handling
exception handling
Manav Dharman
 
Exception handling
Exception handlingException handling
Exception handling
Tata Consultancy Services
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
priyankazope
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
Ganesh kumar reddy
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
Exception
ExceptionException
Exception
Марія Русин
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
Elizabeth alexander
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Prasad Sawant
 
Exception handling
Exception handlingException handling
Exception handling
Ardhendu Nandi
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
ARAFAT ISLAM
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
Md. Tanvir Hossain
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
atozknowledge .com
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 

Similar to Exception handling in java (20)

Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
Nagaraju Pamarthi
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
EduclentMegasoftel
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogrammingoop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
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
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
chauhankapil
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
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
 
java exception.pptx
java exception.pptxjava exception.pptx
java exception.pptx
SukhpreetSingh519414
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & MultithreadingB.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
Assistant Professor, Shri Shivaji Science College, Amravati
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
Nagaraju Pamarthi
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogrammingoop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
gopalrajput11
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
Java Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).pptJava Exception Handling & IO-Unit-3 (1).ppt
Java Exception Handling & IO-Unit-3 (1).ppt
SahilKumar542
 
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
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
SakkaravarthiS1
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
chauhankapil
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
Kuntal Bhowmick
 
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
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024Java-Exception Handling Presentation. 2024
Java-Exception Handling Presentation. 2024
nehakumari0xf
 
Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024Exception Handling In Java Presentation. 2024
Exception Handling In Java Presentation. 2024
kashyapneha2809
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
Ad

More from pooja kumari (9)

Voice recognition
Voice recognitionVoice recognition
Voice recognition
pooja kumari
 
Cyber
CyberCyber
Cyber
pooja kumari
 
Queue
QueueQueue
Queue
pooja kumari
 
Introduction to linked lists
Introduction to linked listsIntroduction to linked lists
Introduction to linked lists
pooja kumari
 
Thread.ppt
Thread.pptThread.ppt
Thread.ppt
pooja kumari
 
Thread.ppt
Thread.pptThread.ppt
Thread.ppt
pooja kumari
 
Applet
AppletApplet
Applet
pooja kumari
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
Sql seuence and sub queries
Sql seuence and sub queriesSql seuence and sub queries
Sql seuence and sub queries
pooja kumari
 
Ad

Recently uploaded (20)

TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 

Exception handling in java

  • 2. Exception Handling in Java  The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.  In this page, we will learn about java exception, its type and the difference between checked and unchecked exceptions. 2
  • 3. What is exception  Dictionary Meaning: Exception is an abnormal condition.  In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.  Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc. 3
  • 4. Advantage of Exception Handling  The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:  statement 1;  statement 2;  statement 3;  statement 4;  statement 5;//exception occurs  statement 6;  statement 7;  statement 8;  statement 9;  statement 10;  Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java. 4
  • 5. Hierarchy of Java Exception classes 5
  • 6. Types of Exception  here are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. The sun microsystem says there are three types of exceptions:  Checked Exception  Unchecked Exception  Error 6
  • 7. Difference between checked and unchecked exceptions  ) Checked Exception  The classes that extend Throwable 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 that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.  3) Error  Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc. 7
  • 8. Common scenarios where exceptions may occur  Scenario where ArithmeticException occurs If we divide any number by zero, there occurs an ArithmeticException.  int a=50/0;//ArithmeticException Scenario where NullPointerException occurs  If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.  String s=null;  System.out.println(s.length());//NullPointerException 8
  • 9. Common scenarios where exceptions may occur  Scenario where NumberFormatException occurs  The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.  String s="abc";  Scenario where ArrayIndexOutOfBoundsException occurs  If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:  int a[]=new int[5];  a[10]=50; //ArrayIndexOutOfBoundsException 9
  • 10. Java Exception Handling Keywords  There are 5 keywords used in java exception handling.  try  catch  finally  throw  throws 10
  • 11. Java try block  Java try block is used to enclose the code that might throw an exception. It must be used within the method.  Java try block must be followed by either catch or finally block.  Syntax of java try-catch  try{  //code that may throw exception  }catch(Exception_class_Name ref){}  Syntax of try-finally block  try{  //code that may throw exception  }finally{} 11
  • 12. Java catch block  Java catch block is used to handle the Exception. It must be used after the try block only.  You can use multiple catch block with a single try.  public class Testtrycatch2{  public static void main(String args[]){  try{  int data=50/0;  }catch(ArithmeticException e){System.out.println(e);}  System.out.println("rest of the code...");  }  } 12
  • 13. output Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code... 13
  • 14. Internal working of java try-catch block 14
  • 15. Java 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 follows try or catch block. 15
  • 17. Java throw keyword  The Java throw keyword is used to explicitly throw an exception.  We can throw either checked or uncheked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception. We will see custom exceptions later.  The syntax of java throw keyword is given below. 17
  • 18. java throw keyword example  public class TestThrow1{  static void validate(int age){  if(age<18)  throw new ArithmeticException("not valid");  else  System.out.println("welcome to vote");  }  public static void main(String args[]){  validate(13);  System.out.println("rest of the code...");  }  } 18
  • 19. Java Exception propagation  class TestExceptionPropagation1{  void m(){  int data=50/0;  }  void n(){  m();  }  void p(){  try{  n();  }catch(Exception e){System.out.println("exception handled");}  }  19
  • 20. Java Exception propagation  public static void main(String args[]){  TestExceptionPropagation1 obj=new TestExceptionPropagation1();  obj.p();  System.out.println("normal flow...");  }  } 20
  • 22. Java Exception propagation  In the above example exception occurs in m() method where it is not handled,so it is propagated to previous n() method where it is not handled, again it is propagated to p() method where exception is handled.  Exception can be handled in any method in call stack either in main() method,p() method,n() method or m() method. 22
  • 23. Java throws keyword  The Java throws keyword is used to declare an exception. It 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.  Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used. 23
  • 24. Difference between throw and throws in Java throw  Java throw keyword is used to explicitly throw an exception.  Checked exception cannot be propagated using throw only.  Throw is followed by an instance.  Throw is used within the method.  You cannot throw multiple exceptions. throws  Java throws keyword is used to declare an exception.  Checked exception can be propagated with throws.  Throws is followed by class.  Throws is used with the method signature.  You can declare multiple exceptions e.g. public void method()throws IOException,SQLException. 24
  翻译: