SlideShare a Scribd company logo
12/6/19
Java exceptions
Ivelin Yanev
12/6/19
What is an Exception?
An exception is an unwanted or unexpected event, which occurs during the
execution of a program.
2
12/6/19
Exception Hierarchy
12/6/19
Type of exceptions in Java
1. Checked Exceptions - a checked exception is an exception that is typically a
user error or a problem that cannot be foreseen by the programmer. These
exceptions cannot simply be ignored at the time of compilation.
2. Unchecked Exceptions - these exceptions are not checked at compile-time
so compiler does not check whether the programmer has handled them or
not but it’s the responsibility of the programmer to handle these exceptions
and provide a safe exit.
3. Errors - these are not exceptions at all, but problems that arise beyond the
control of the user or the programmer.
12/6/19
Difference between error and exception
1. Errors - indicates serious problems that a reasonable application should not
try to catch.
2. Exceptions - indicates conditions that a reasonable application might want to
catch.
Demo → https://www.geeksforgeeks.org/errors-v-s-exceptions-in-java/
12/6/19
Exceptions Methods
●
public String getMessage() - returns a detailed message about the exception
that has occurred.
●
public Throwable getCause() - returns the cause of the exception as
represented by a Throwable object.
●
public void printStackTrace() - prints the result of toString along with the stack
trace.
●
public StackTraceElement [] getStackTrace() - returns an array containing each
element on the stack trace.
12/6/19
Handling Exceptions
➱
Catching Exceptions – demo
➱
Multiple catch Blocks – demo
try {
//Protected code
}catch(ExceptionName e1) {
//Catch block
}
try {
//Protected code
}catch(ExceptionType1 e1)
{
//Catch block
}catch(ExceptionType2 e2)
{
//Catch block
}catch(ExceptionType3 e3)
{
//Catch block
}
12/6/19
Handling Exceptions
➱
Catching multiple type of exceptions – demo
➱
The throws/throw Keywords – demo
catch (IOException|FileNotFoundException ex){
logger.log(ex);
throw ex;
}
import java.io.*;
public class className {
public void deposit(double amount) throws RemoteException{
// Method implementation
throw new RemoteException();
}
//Remainder of class definition
}
12/6/19
Handling Exceptions
➱
The finally block - a finally block appears at the end of the catch blocks. -
demo
➱
The try-with-resources - demo
try {
//Protected code
}catch(ExceptionType1 e1) {
//Catch block
}catch(ExceptionType2 e2) {
//Catch block
}catch(ExceptionType3 e3) {
//Catch block
}finally {
//The finally block always executes.
}
try(FileReader fr=new FileReader("file path")){
//use the resource
}catch(){
//body of catch
}
12/6/19
Handling Exceptions
➱
User-defined Exceptions - all exceptions must be a child of Throwable.
class MyException extends Exception{
//......
}
12/6/19
Best practices
“When used to best advantage, exceptions can improve a program’s readability, reliability, and
maintainability. When used improperly, they can have the opposite effect.”
– Effective Java

More Related Content

What's hot (20)

PDF
Creating your own exception
TharuniDiddekunta
 
PPT
Exception handling
Karthik Sekar
 
PDF
Exception handling
Muthukumaran Subramanian
 
PPT
Savitch Ch 16
Terry Yoast
 
PPTX
Java
SangeethaSasi1
 
PPSX
Exception hierarchy
Ashfaaq Mahroof
 
PPTX
Exception handling
Shashwat Shriparv
 
PPTX
Exceptions in java
Sameer Patil
 
PDF
Java Pitfalls and Good-to-Knows
Miquel Martin
 
PPTX
Satish training ppt
satish lariya
 
PPTX
Exception handling in java
Kavitha713564
 
PDF
summarizer16fev16_Exceptions
Filipe Morais Jorge
 
PPTX
Java Exceptions and Exception Handling
MaqdamYasir
 
PPTX
exception handling in java
Abinaya B
 
PDF
Exception handling basic
TharuniDiddekunta
 
PPT
Chap12
Terry Yoast
 
PPT
9781439035665 ppt ch11
Terry Yoast
 
PPTX
Introduction to java exceptions
Sujit Kumar
 
PPTX
Chapter 5
siragezeynu
 
Creating your own exception
TharuniDiddekunta
 
Exception handling
Karthik Sekar
 
Exception handling
Muthukumaran Subramanian
 
Savitch Ch 16
Terry Yoast
 
Exception hierarchy
Ashfaaq Mahroof
 
Exception handling
Shashwat Shriparv
 
Exceptions in java
Sameer Patil
 
Java Pitfalls and Good-to-Knows
Miquel Martin
 
Satish training ppt
satish lariya
 
Exception handling in java
Kavitha713564
 
summarizer16fev16_Exceptions
Filipe Morais Jorge
 
Java Exceptions and Exception Handling
MaqdamYasir
 
exception handling in java
Abinaya B
 
Exception handling basic
TharuniDiddekunta
 
Chap12
Terry Yoast
 
9781439035665 ppt ch11
Terry Yoast
 
Introduction to java exceptions
Sujit Kumar
 
Chapter 5
siragezeynu
 

Similar to Java exeptions (20)

PPTX
Exception Handling.pptx
primevideos176
 
PPT
Exception handling
Tata Consultancy Services
 
PDF
Java exceptions
Pawan Kumar
 
PPTX
EXCEPTION HANDLING in prograaming
MuskanNazeer
 
PPTX
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPT
Java: Exception
Tareq Hasan
 
PDF
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
PPTX
java exception.pptx
SukhpreetSingh519414
 
PPTX
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
PPTX
Exception handling in java
Elizabeth alexander
 
PPTX
Interface andexceptions
saman Iftikhar
 
PPTX
Java exeception handling
baabtra.com - No. 1 supplier of quality freshers
 
PPT
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
Exception handling in java
pooja kumari
 
PPTX
Chap2 exception handling
raksharao
 
PPTX
Java-Unit 3- Chap2 exception handling
raksharao
 
PPT
06 exceptions
Waheed Warraich
 
Exception Handling.pptx
primevideos176
 
Exception handling
Tata Consultancy Services
 
Java exceptions
Pawan Kumar
 
EXCEPTION HANDLING in prograaming
MuskanNazeer
 
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
Java exception handling
BHUVIJAYAVELU
 
Java: Exception
Tareq Hasan
 
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
java exception.pptx
SukhpreetSingh519414
 
using Java Exception Handling in Java.pptx
AshokRachapalli1
 
Exception handling in java
Elizabeth alexander
 
Interface andexceptions
saman Iftikhar
 
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
ssuserf45a65
 
UNIT III 2021R.pptx
RDeepa9
 
UNIT III 2021R.pptx
RDeepa9
 
Exception handling in java
pooja kumari
 
Chap2 exception handling
raksharao
 
Java-Unit 3- Chap2 exception handling
raksharao
 
06 exceptions
Waheed Warraich
 
Ad

More from Ivelin Yanev (11)

PDF
Quarkus Extensions Turbocharge for Java Microservices.pdf
Ivelin Yanev
 
PDF
Empowering Your Java Applications with Quarkus. A New Era of Fast, Efficient,...
Ivelin Yanev
 
PDF
Project Loom
Ivelin Yanev
 
PPTX
Building flexible ETL pipelines with Apache Camel on Quarkus
Ivelin Yanev
 
PDF
Git collaboration
Ivelin Yanev
 
PDF
Introducing java oop concepts
Ivelin Yanev
 
PDF
Introducing generic types
Ivelin Yanev
 
PDF
Java features. Java 8, 9, 10, 11
Ivelin Yanev
 
PDF
Design principles
Ivelin Yanev
 
PDF
Java 9 modularity+
Ivelin Yanev
 
PDF
Intoduction Internet of Things
Ivelin Yanev
 
Quarkus Extensions Turbocharge for Java Microservices.pdf
Ivelin Yanev
 
Empowering Your Java Applications with Quarkus. A New Era of Fast, Efficient,...
Ivelin Yanev
 
Project Loom
Ivelin Yanev
 
Building flexible ETL pipelines with Apache Camel on Quarkus
Ivelin Yanev
 
Git collaboration
Ivelin Yanev
 
Introducing java oop concepts
Ivelin Yanev
 
Introducing generic types
Ivelin Yanev
 
Java features. Java 8, 9, 10, 11
Ivelin Yanev
 
Design principles
Ivelin Yanev
 
Java 9 modularity+
Ivelin Yanev
 
Intoduction Internet of Things
Ivelin Yanev
 
Ad

Recently uploaded (20)

PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 

Java exeptions

  • 2. 12/6/19 What is an Exception? An exception is an unwanted or unexpected event, which occurs during the execution of a program. 2
  • 4. 12/6/19 Type of exceptions in Java 1. Checked Exceptions - a checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. These exceptions cannot simply be ignored at the time of compilation. 2. Unchecked Exceptions - these exceptions are not checked at compile-time so compiler does not check whether the programmer has handled them or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe exit. 3. Errors - these are not exceptions at all, but problems that arise beyond the control of the user or the programmer.
  • 5. 12/6/19 Difference between error and exception 1. Errors - indicates serious problems that a reasonable application should not try to catch. 2. Exceptions - indicates conditions that a reasonable application might want to catch. Demo → https://www.geeksforgeeks.org/errors-v-s-exceptions-in-java/
  • 6. 12/6/19 Exceptions Methods ● public String getMessage() - returns a detailed message about the exception that has occurred. ● public Throwable getCause() - returns the cause of the exception as represented by a Throwable object. ● public void printStackTrace() - prints the result of toString along with the stack trace. ● public StackTraceElement [] getStackTrace() - returns an array containing each element on the stack trace.
  • 7. 12/6/19 Handling Exceptions ➱ Catching Exceptions – demo ➱ Multiple catch Blocks – demo try { //Protected code }catch(ExceptionName e1) { //Catch block } try { //Protected code }catch(ExceptionType1 e1) { //Catch block }catch(ExceptionType2 e2) { //Catch block }catch(ExceptionType3 e3) { //Catch block }
  • 8. 12/6/19 Handling Exceptions ➱ Catching multiple type of exceptions – demo ➱ The throws/throw Keywords – demo catch (IOException|FileNotFoundException ex){ logger.log(ex); throw ex; } import java.io.*; public class className { public void deposit(double amount) throws RemoteException{ // Method implementation throw new RemoteException(); } //Remainder of class definition }
  • 9. 12/6/19 Handling Exceptions ➱ The finally block - a finally block appears at the end of the catch blocks. - demo ➱ The try-with-resources - demo try { //Protected code }catch(ExceptionType1 e1) { //Catch block }catch(ExceptionType2 e2) { //Catch block }catch(ExceptionType3 e3) { //Catch block }finally { //The finally block always executes. } try(FileReader fr=new FileReader("file path")){ //use the resource }catch(){ //body of catch }
  • 10. 12/6/19 Handling Exceptions ➱ User-defined Exceptions - all exceptions must be a child of Throwable. class MyException extends Exception{ //...... }
  • 11. 12/6/19 Best practices “When used to best advantage, exceptions can improve a program’s readability, reliability, and maintainability. When used improperly, they can have the opposite effect.” – Effective Java