SlideShare une entreprise Scribd logo
1  sur  17
Java SE 11 Exception
Handling
BY: ASHWIN SHIV
NIT DELHI
Exception Handling
Introduction
 Exceptions in Java are any abnormal, unexpected events or extra-ordinary
conditions that may occur at runtime.
 Java Exception Handling is used to handle error conditions in a program
systemically by taking the necessary action.
 The java.lang.Throwable class and all its subclasses are collectively the
exception classes.
Class Hierarchy
Throwable has two subclasses: Exception and Error
What Happens when an Exception
occurs in a Java Program
 When an exception occurs, the method from which the exception
originated, creates a special object known as exception object and hands
it off to the Runtime System.
 The exception object contains information about the exception, and the
state of the program when the exception occurred.
 Creating an exception object and handing it to the runtime system is called
throwing an exception.
 You can manually throw an exception by identifying a scenario you
considered an exception, but in many cases, it is the Java Virtual
Machine(JVM) throwing exceptions manually
Searching the Call Stack for the
Exception Handler
try catch Clause
 If an exception occurs at the particular statement of try block,
the rest of the block code will not execute and code will fall
through to the catch statement.
 Syntax of try-catch:
 Unlike other statements such as if and else,the enclosing
braces {} sets are required in this case.
try catch finally clause
 The finally clause purpose is to clean up the resources.It will always get executed whether
an exception occurs in the try clause or not.
 You can declare a try-finally block also and you can also declare try-catch-finally together
also.
 What you cannot do is just declaring a try clause without a catch,or finally clause.It will
result in compiler error.
Declaring try-finally Declaring try-catch-finally
More on try-catch finally
 You can put a nested try-catch in outer try block or outer catch block or
outer finally block.
throw clause
 A program can explicitly throw an exception using the throw keyword besides the
implicit exception thrown.
 Syntax:
throws clause
 throws clause 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:
Unchecked Exceptions
 Unchecked Exceptions are those exceptions in which the compiler does
not force you to write code to check for the exception or specify the
exception.
 In other words, Unchecked Exceptions are those classes which inherit
RuntimeException.
 Eg: RuntimeException, ArithmeticException,
ArrayIndexOutOfBoundsException etc.
Unchecked Exceptions in Work
Output:
In this example,the program
abruptly ends as the exception is
not handled
Output
In this example,the program continues
after the exception has been caught
and handled by the catch clause so the
program will continue
Checked Exceptions
 A Checked Exception is a special designation, for a group of
exceptions that the compiler forces compliance of ‘The Catch
or Specify Requirement’.
 Catch or Specify Requirement: It states that code that might
throw certain exceptions, must be enclosed by either of the
following:
 A try statement that catches the exception. The try must provide an
appropriate handler for the exception.
 A method that specifies that it can throw Exception. The method must
provide a throws clause that lists the exception or an appropriate
exception type.
Checked Exceptions In Work
Output Output
In this example, the program abruptly
ends as the checked exception is not
handled.
In this example,the program continues
after the checked exception has been
caught and handled by the catch clause
so the program will continue.
Table to explain which Exceptions are
Checked or which are Unchecked
Thank You

Contenu connexe

Tendances

Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
Alpesh Oza
 

Tendances (20)

Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
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
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in 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
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptional Handling in Java
Exceptional Handling in JavaExceptional Handling in Java
Exceptional Handling in Java
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
Java review: try catch
Java review: try catchJava review: try catch
Java review: try catch
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
summarizer16fev16_Exceptions
summarizer16fev16_Exceptionssummarizer16fev16_Exceptions
summarizer16fev16_Exceptions
 

Similaire à Java SE 11 Exception Handling

Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
Rakesh Madugula
 
Unit5 java
Unit5 javaUnit5 java
Unit5 java
mrecedu
 

Similaire à Java SE 11 Exception Handling (20)

Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Exception handling
Exception handlingException handling
Exception handling
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Exception handling basic
Exception handling basicException handling basic
Exception handling basic
 
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.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
exceptions in java
exceptions in javaexceptions in java
exceptions in java
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.ppt
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
 
Unit5 java
Unit5 javaUnit5 java
Unit5 java
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
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
 
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
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Java SE 11 Exception Handling

  • 1. Java SE 11 Exception Handling BY: ASHWIN SHIV NIT DELHI
  • 3. Introduction  Exceptions in Java are any abnormal, unexpected events or extra-ordinary conditions that may occur at runtime.  Java Exception Handling is used to handle error conditions in a program systemically by taking the necessary action.  The java.lang.Throwable class and all its subclasses are collectively the exception classes.
  • 4. Class Hierarchy Throwable has two subclasses: Exception and Error
  • 5. What Happens when an Exception occurs in a Java Program  When an exception occurs, the method from which the exception originated, creates a special object known as exception object and hands it off to the Runtime System.  The exception object contains information about the exception, and the state of the program when the exception occurred.  Creating an exception object and handing it to the runtime system is called throwing an exception.  You can manually throw an exception by identifying a scenario you considered an exception, but in many cases, it is the Java Virtual Machine(JVM) throwing exceptions manually
  • 6. Searching the Call Stack for the Exception Handler
  • 7. try catch Clause  If an exception occurs at the particular statement of try block, the rest of the block code will not execute and code will fall through to the catch statement.  Syntax of try-catch:  Unlike other statements such as if and else,the enclosing braces {} sets are required in this case.
  • 8. try catch finally clause  The finally clause purpose is to clean up the resources.It will always get executed whether an exception occurs in the try clause or not.  You can declare a try-finally block also and you can also declare try-catch-finally together also.  What you cannot do is just declaring a try clause without a catch,or finally clause.It will result in compiler error. Declaring try-finally Declaring try-catch-finally
  • 9. More on try-catch finally  You can put a nested try-catch in outer try block or outer catch block or outer finally block.
  • 10. throw clause  A program can explicitly throw an exception using the throw keyword besides the implicit exception thrown.  Syntax:
  • 11. throws clause  throws clause 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:
  • 12. Unchecked Exceptions  Unchecked Exceptions are those exceptions in which the compiler does not force you to write code to check for the exception or specify the exception.  In other words, Unchecked Exceptions are those classes which inherit RuntimeException.  Eg: RuntimeException, ArithmeticException, ArrayIndexOutOfBoundsException etc.
  • 13. Unchecked Exceptions in Work Output: In this example,the program abruptly ends as the exception is not handled Output In this example,the program continues after the exception has been caught and handled by the catch clause so the program will continue
  • 14. Checked Exceptions  A Checked Exception is a special designation, for a group of exceptions that the compiler forces compliance of ‘The Catch or Specify Requirement’.  Catch or Specify Requirement: It states that code that might throw certain exceptions, must be enclosed by either of the following:  A try statement that catches the exception. The try must provide an appropriate handler for the exception.  A method that specifies that it can throw Exception. The method must provide a throws clause that lists the exception or an appropriate exception type.
  • 15. Checked Exceptions In Work Output Output In this example, the program abruptly ends as the checked exception is not handled. In this example,the program continues after the checked exception has been caught and handled by the catch clause so the program will continue.
  • 16. Table to explain which Exceptions are Checked or which are Unchecked