SlideShare una empresa de Scribd logo
1 de 11
Exceptions
Chapter 15
15
Throwing and Catching Exceptions
When a program runs into a problem
that it cannot handle, it throws an
exception.
Exceptions are either caught (handled) or
unhandled.
Unhandled exceptions cause the
application to exit.
15
Unhandled Exceptions
When Java encounters an unhandled
exception (e.g., divide by zero), the compiler
provides information that can help you find
the source of the error.
15
When an Exception is Thrown
Processing stops at point of exception.
Java Virtual Machine (JVM) looks for a
catch block to handle the exception.
JVM unwinds the stack, or retraces its
steps searching for a catch block.
If no catch block is found, the program
terminates with the default handler.
15
Catching Exceptions
A try / catch block is the general
approach to exception handling.
The try block contains the code that
might throw an exception.
The catch block catches and handles the
exception, if thrown.
15
Throwing Exceptions
Use the throw keyword when throwing
the exception.
if ( divisor == 0 )
{
throw new ArithmeticException();
}
15
Exception Classifications
All exceptions ultimately derive from
Throwable class, which is divided
into Error and Exception subclasses
Error exceptions are thrown by Java
system internal errors (e.g., “out of
memory”).
Error exceptions are never thrown by
Java programmers.
15
Exception Class
The Exception class is divided into
IOException and RunTimeException.
IOException problems are typically
beyond your control (e.g., the disk is full).
RunTimeException usually indicates a
programmer error (e.g., trying to write past
the end of an array).
15
CustomExceptions
Creating your own exception classes
allows you to throw more specific
exceptions.
Exceptions can be used polymorphically.
Multiple exception handling must be
coded in order from specific to more
general.
An exception class derived from a more general
exception must be caught first.
15
The throws Keyword
A method can declare that it throws a
certain type of exception using the
throws keyword.
“I am the readFile method and I might
throw an IOException.”
public void readFile(String
fileName) throws IOException
15
The finally Keyword
The finally keyword works in
conjunction with a try / catch block.
finally “cleans up” after an exception
Important to include finally block to
limit the chance of resource leaks due to
open connections or files

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

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
 
exception handling in java
exception handling in java exception handling in java
exception 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
 
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 in java
Exception handling  in javaException handling  in java
Exception handling in java
 
exceptions in java
exceptions in javaexceptions in java
exceptions in java
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Exception
ExceptionException
Exception
 
Exception handling
Exception handlingException handling
Exception handling
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
Java Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-KnowsJava Pitfalls and Good-to-Knows
Java Pitfalls and Good-to-Knows
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 

Destacado (8)

Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Enterprise Skills
Enterprise SkillsEnterprise Skills
Enterprise Skills
 
Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Key enterprise skills
Key enterprise skillsKey enterprise skills
Key enterprise skills
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 

Similar a Chapter 15

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 MultithreadingSakkaravarthiS1
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxprimevideos176
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception HandlingAshwin Shiv
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAmbigaMurugesan
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
OCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 ExceptionsOCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 Exceptionsİbrahim Kürce
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .happycocoman
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptionssaman Iftikhar
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handlingraksharao
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingraksharao
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaARAFAT ISLAM
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in javajunnubabu
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapooja kumari
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 

Similar a Chapter 15 (20)

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 basic
Exception handling basicException handling basic
Exception handling basic
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 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
 
OCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 ExceptionsOCA Java SE 8 Exam Chapter 6 Exceptions
OCA Java SE 8 Exam Chapter 6 Exceptions
 
8.Exception handling latest(MB).ppt .
8.Exception handling latest(MB).ppt      .8.Exception handling latest(MB).ppt      .
8.Exception handling latest(MB).ppt .
 
JAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdfJAVA PPT -4 BY ADI.pdf
JAVA PPT -4 BY ADI.pdf
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
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
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
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
 

Más de Graham Royce (20)

Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Chapter 09
Chapter 09Chapter 09
Chapter 09
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Chapter 07
Chapter 07Chapter 07
Chapter 07
 
Chapter 06
Chapter 06Chapter 06
Chapter 06
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter 02
Chapter 02Chapter 02
Chapter 02
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
13 java in oracle
13 java in oracle13 java in oracle
13 java in oracle
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
My 3 min pitch pack
My 3 min pitch packMy 3 min pitch pack
My 3 min pitch pack
 
Things to do
Things to doThings to do
Things to do
 
House in order
House in orderHouse in order
House in order
 
Blank 10 all you need
Blank 10 all you needBlank 10 all you need
Blank 10 all you need
 
Goals
GoalsGoals
Goals
 
Idea's1 only
Idea's1 onlyIdea's1 only
Idea's1 only
 
New frontiers motivation
New frontiers motivationNew frontiers motivation
New frontiers motivation
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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 Processorsdebabhi2
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
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 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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 WorkerThousandEyes
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Último (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Chapter 15

  • 2. 15 Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions are either caught (handled) or unhandled. Unhandled exceptions cause the application to exit.
  • 3. 15 Unhandled Exceptions When Java encounters an unhandled exception (e.g., divide by zero), the compiler provides information that can help you find the source of the error.
  • 4. 15 When an Exception is Thrown Processing stops at point of exception. Java Virtual Machine (JVM) looks for a catch block to handle the exception. JVM unwinds the stack, or retraces its steps searching for a catch block. If no catch block is found, the program terminates with the default handler.
  • 5. 15 Catching Exceptions A try / catch block is the general approach to exception handling. The try block contains the code that might throw an exception. The catch block catches and handles the exception, if thrown.
  • 6. 15 Throwing Exceptions Use the throw keyword when throwing the exception. if ( divisor == 0 ) { throw new ArithmeticException(); }
  • 7. 15 Exception Classifications All exceptions ultimately derive from Throwable class, which is divided into Error and Exception subclasses Error exceptions are thrown by Java system internal errors (e.g., “out of memory”). Error exceptions are never thrown by Java programmers.
  • 8. 15 Exception Class The Exception class is divided into IOException and RunTimeException. IOException problems are typically beyond your control (e.g., the disk is full). RunTimeException usually indicates a programmer error (e.g., trying to write past the end of an array).
  • 9. 15 CustomExceptions Creating your own exception classes allows you to throw more specific exceptions. Exceptions can be used polymorphically. Multiple exception handling must be coded in order from specific to more general. An exception class derived from a more general exception must be caught first.
  • 10. 15 The throws Keyword A method can declare that it throws a certain type of exception using the throws keyword. “I am the readFile method and I might throw an IOException.” public void readFile(String fileName) throws IOException
  • 11. 15 The finally Keyword The finally keyword works in conjunction with a try / catch block. finally “cleans up” after an exception Important to include finally block to limit the chance of resource leaks due to open connections or files