SlideShare a Scribd company logo
Presentation on:-
Exception Handling In JAVA
Presented by:-
ANTARA MITRA
ANUPRIYA
ARDHENDU NANDI
Exception
 An exception is a problem that arises during the time
of execution of program. An exception can occur for
many different reasons, including the following.
A user has enter invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of
communicatons,or the JVM has run out of memory.
Some of these exception are caused by user error,
others by programmer error, and others by physical
resources, that have failed in some manner.
Other exception
 It is possible to define two categories of Exception
and Errors.
o JVM Exceptions:- These are exceptions/errors that are
exclusively or logically thrown by the JVM.
o Example:- Nullpointer Exception, ArrayIndexOutof Bounds
Exception,Classcast Exception.
o Programmatic Exception:- These Exception are thrown
explicitly by the application or the API programmers.
o Example:- IllegalArgument Exception, IllegalState Exception.
Categories Exception
 Checked Exception:- A checked Excption is an exception that is
typically a user or a problem that cannot be forseen .By the programmer.
For example if a file is to be opened, but the file cannot be found, an
exception occurs. These exceptions cannot simply be ignored at the time of
compilation.
 Runtime Exceptions:- A runtime exception is an exception that
occurs that probably could have been avoided by the programmer. As
opposed to checked exception, runtime exceptions are ignored at the time
of compilation.
 Errors:- These are not exceptions at all ,but problems that arise
beyond the control of the user or the programmer. Errors are
typically ignored in your code because you can really do anything
about an error. For example, if a stack overflow occurs, an error will
arise. They are also ignored at the time of complation.
Exception hierarchy
 All exception classes are subtypes of the java.lang.Exception
class. The exception class is a subclass of the Throwable
class. Other than the exception class there is another subclass
called error which is derived from throwable class.
 Error are not normally trapped from the java programs. These
conditions normally happen in case of sever failures, which are
not handled by the java programs. Error are Generated to
indicate errors generated by the runtime environment.
Example: JVM is out of memory.
 Normally programs cannot recover from errors.
 The exception class has two main subclasses:
 IOException class
 Runtime Exception Class.
Fig : Exception hierarchy.
Catching exception
 A method catches an exception using a combination of the try
and catch keywords.
 A try/catch block is placed around the code that might generate an
exception.
 Code within a try/catch block is referred to as protected code, and
the syntax for using try/catch looks like the following:
Try
{
//protected code
}catch(ExceptionName e1)
{
//catch block
}
 A catch statement involves declaring the type of exception you
are trying to catch. If an exception occurs in protected code, the
catch block (or blocks ) that follows the try is checked.
Internal working of java try-catch block
Example of Try-catch
Input
public class Testtrycatch{
public static void main(String args[]){
try{
int data=50/0;
}catch(Exception e)
{
System.out.println(e);
}
System.out.println("rest of the code...");
}
}
output
Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
Multiple catch block
 If you have to perform different tasks at the occurrence of different
Exceptions, use java multi catch block.
Syntax :-
Try
{
//protected code
}Catch(ExceptionType1 e1)
{
//catch block
}Catch(ExceptionType2 e2)
{
//catch block
}Catch(ExceptionType3 e3)
{
//catch block
}
 Rule1: At a time only one Exception is occured and at a time only one catch block
is executed.
 Rule2: All catch blocks must be ordered from most specific to most general i.e. catch for
ArithmeticException must come before catch for Exception .
Example of multiple catch block
Input
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException
e){System.out.println("task1 is
completed");}
catch(ArrayIndexOutOfBoundsException
e){System.out.println("task 2
completed");}
catch(Exception
e){System.out.println("common task
completed");}
System.out.println("rest of the code...");
}
}
output
task1 completed
rest of the code...
I
input
class TestMultipleCatchBlock1{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(Exception
e){System.out.println("common task
completed");}
catch(ArithmeticException
e){System.out.println("task1 is
completed");}
catch(ArrayIndexOutOfBoundsException
e){System.out.println("task 2
completed");}
System.out.println("rest of the code...");
}
}
output
Compile-time error
Throws/throw
 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.
 Syntax of throws keywod is
return_type method_name() throws exception_class_name{
//method code
}
 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.
 Syntax of throw is throw exception;
Example of Exception handling
Input
public class test exception{
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...");
}
}
output
Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
Example of throws & throw
Input
import java.io.IOException;
class Testthrows1{
void m()throws IOException{
throw new IOException("device error");//chec
ked exception
}
void n()throws IOException{
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("excep
tion handled");}
}
public static void main(String args[]){
Testthrows1 obj=new Testthrows1();
obj.p();
System.out.println("normal flow...");
}
}
Output
exception handled
normal flow…….
Input
public class TestThrow1{
static void validate(int age){
if(age<18)
throw new ArithmeticException("not v
alid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
validate(13);
System.out.println("rest of the code..."
);
}
}
Output
Exception in thread main
java.lang.ArithmeticException:not valid
Advantages
The advantage of Exception handling are:-
 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;
Thank you

More Related Content

What's hot

Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
vanithaRamasamy
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 
Java applets
Java appletsJava applets
Java applets
Pihu Goel
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
Lovely Professional University
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
PRINCE KUMAR
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)
Sujit Majety
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nochiketa Chakraborty
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 

What's hot (20)

Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java applets
Java appletsJava applets
Java applets
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java exception
Java exception Java exception
Java exception
 
Introduction to java (revised)
Introduction to java (revised)Introduction to java (revised)
Introduction to java (revised)
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 

Viewers also liked

Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Prasad Sawant
 
12 exception handling
12 exception handling12 exception handling
12 exception handling
Arriz San Juan
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
priyankazope
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Exception
ExceptionException
Java Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-KnowsJava Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-Knows
Miquel Martin
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
ankitgarg_er
 
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
SURIT DATTA
 

Viewers also liked (10)

Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
12 exception handling
12 exception handling12 exception handling
12 exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Exception
ExceptionException
Exception
 
Java Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-KnowsJava Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-Knows
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 

Similar to Exception handling

UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
EduclentMegasoftel
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
Pawan Kumar
 
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.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
primevideos176
 
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
 
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.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
Nagaraju Pamarthi
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
saman Iftikhar
 
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
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
junnubabu
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
Abhishek Pachisia
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
ARAFAT ISLAM
 
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
 
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
pooja kumari
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
pooja kumari
 
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
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
AmbigaMurugesan
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Kavitha713564
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 

Similar to Exception handling (20)

UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Java exceptions
Java exceptionsJava exceptions
Java exceptions
 
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.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
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;...
 
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
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
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
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
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
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
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
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 

Recently uploaded

Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 

Recently uploaded (20)

Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 

Exception handling

  • 1. Presentation on:- Exception Handling In JAVA Presented by:- ANTARA MITRA ANUPRIYA ARDHENDU NANDI
  • 2. Exception  An exception is a problem that arises during the time of execution of program. An exception can occur for many different reasons, including the following. A user has enter invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communicatons,or the JVM has run out of memory. Some of these exception are caused by user error, others by programmer error, and others by physical resources, that have failed in some manner.
  • 3. Other exception  It is possible to define two categories of Exception and Errors. o JVM Exceptions:- These are exceptions/errors that are exclusively or logically thrown by the JVM. o Example:- Nullpointer Exception, ArrayIndexOutof Bounds Exception,Classcast Exception. o Programmatic Exception:- These Exception are thrown explicitly by the application or the API programmers. o Example:- IllegalArgument Exception, IllegalState Exception.
  • 4. Categories Exception  Checked Exception:- A checked Excption is an exception that is typically a user or a problem that cannot be forseen .By the programmer. For example if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.  Runtime Exceptions:- A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exception, runtime exceptions are ignored at the time of compilation.  Errors:- These are not exceptions at all ,but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can really do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of complation.
  • 5. Exception hierarchy  All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called error which is derived from throwable class.  Error are not normally trapped from the java programs. These conditions normally happen in case of sever failures, which are not handled by the java programs. Error are Generated to indicate errors generated by the runtime environment. Example: JVM is out of memory.  Normally programs cannot recover from errors.  The exception class has two main subclasses:  IOException class  Runtime Exception Class.
  • 6. Fig : Exception hierarchy.
  • 7. Catching exception  A method catches an exception using a combination of the try and catch keywords.  A try/catch block is placed around the code that might generate an exception.  Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following: Try { //protected code }catch(ExceptionName e1) { //catch block }  A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks ) that follows the try is checked.
  • 8. Internal working of java try-catch block
  • 9. Example of Try-catch Input public class Testtrycatch{ public static void main(String args[]){ try{ int data=50/0; }catch(Exception e) { System.out.println(e); } System.out.println("rest of the code..."); } } output Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...
  • 10. Multiple catch block  If you have to perform different tasks at the occurrence of different Exceptions, use java multi catch block. Syntax :- Try { //protected code }Catch(ExceptionType1 e1) { //catch block }Catch(ExceptionType2 e2) { //catch block }Catch(ExceptionType3 e3) { //catch block }  Rule1: At a time only one Exception is occured and at a time only one catch block is executed.  Rule2: All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception .
  • 11. Example of multiple catch block Input public class TestMultipleCatchBlock{ public static void main(String args[]){ try{ int a[]=new int[5]; a[5]=30/0; } catch(ArithmeticException e){System.out.println("task1 is completed");} catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");} catch(Exception e){System.out.println("common task completed");} System.out.println("rest of the code..."); } } output task1 completed rest of the code... I input class TestMultipleCatchBlock1{ public static void main(String args[]){ try{ int a[]=new int[5]; a[5]=30/0; } catch(Exception e){System.out.println("common task completed");} catch(ArithmeticException e){System.out.println("task1 is completed");} catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");} System.out.println("rest of the code..."); } } output Compile-time error
  • 12. Throws/throw  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.  Syntax of throws keywod is return_type method_name() throws exception_class_name{ //method code }  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.  Syntax of throw is throw exception;
  • 13. Example of Exception handling Input public class test exception{ 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..."); } } output Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...
  • 14. Example of throws & throw Input import java.io.IOException; class Testthrows1{ void m()throws IOException{ throw new IOException("device error");//chec ked exception } void n()throws IOException{ m(); } void p(){ try{ n(); }catch(Exception e){System.out.println("excep tion handled");} } public static void main(String args[]){ Testthrows1 obj=new Testthrows1(); obj.p(); System.out.println("normal flow..."); } } Output exception handled normal flow……. Input public class TestThrow1{ static void validate(int age){ if(age<18) throw new ArithmeticException("not v alid"); else System.out.println("welcome to vote"); } public static void main(String args[]){ validate(13); System.out.println("rest of the code..." ); } } Output Exception in thread main java.lang.ArithmeticException:not valid
  • 15. Advantages The advantage of Exception handling are:-  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;