SlideShare a Scribd company logo
CIS 2403- Object Oriented Programming 1
Exception Handling
CIS 2403
Object Oriented Programming
CIS 2403- Object Oriented Programming 2
Understanding Exceptions
• An exception is an
event that occurs
during the execution of
a program that
disrupts the normal
flow of instructions.
• Exceptions are caused
by errors in program
logic or insufficient
system resources.
CIS 2403- Object Oriented Programming 3
Example
CIS 2403- Object Oriented Programming 4
Understanding Exceptions
• The programs you write can generate
many types of potential exceptions,
including when:
 Your program asks for user input, but the user enters
invalid data.
 The program attempts to divide an integer by zero.
 You attempt to access an array with a subscript
that is too large or too small.
 You calculate a value that is too large for the
answer’s variable type.
CIS 2403- Object Oriented Programming 5
Understanding Exceptions
• These errors are called exceptions
because presumably they are not usual
occurrences; they are “exceptional.”
• The object-oriented techniques used to
manage such errors make up the group of
methods known as exception handling. If
you do not handle an exception, the
running program terminates abruptly.
CIS 2403- Object Oriented Programming 6
Traditional Error Handling
• Programmers had to deal with error conditions
long before object-oriented methods were
conceived
• This code successfully prevents division by zero,
but it does not really “handle an exception”
because no Exception class object is created.
CIS 2403- Object Oriented Programming 7
Traditional Error Handling
• If a user enters 0 for gallonsOfGas in only one
case out of 1,000, then the if statement is
executed unnecessarily 999 times!
• In that case, it is more efficient to eliminate the if
test and instantiate an Exception object when
needed.
CIS 2403- Object Oriented Programming 8
Handling Exceptions
• A program can catch exceptions by using a
combination of the try, catch, and finally
blocks.
 The try block identifies a block of code in
which an exception can occur.
 The catch block identifies a block of code,
known as an exception handler, that can
handle a particular type of exception.
 The finally block identifies a block of code
that is guaranteed to execute.
CIS 2403- Object Oriented Programming 9
The Try Block
• The first step is to enclose the code that might
throw an exception within a try block.
• To associate an exception handler with a
try block, you must put a catch block after
it
CIS 2403- Object Oriented Programming 10
The catch Blocks
• You associate exception handlers with a try
block by providing one or more catch blocks
directly after the try block.
• The catch block contains code that is executed if
and when the exception handler is invoked.
CIS 2403- Object Oriented Programming 11
The finally Block
• The finally block always executes when the try
block exits.
CIS 2403- Object Oriented Programming 12
Let’s Apply
CIS 2403- Object Oriented Programming 13
Resources
• Farrell.. Microsoft® Visual C# 2012: An
Introduction to Object-Oriented
Programming, International Edition, 5th
Edition. Cengage Learning UK, 2013.
• Oracle, Java Documentation:
https://docs.oracle.com/javase/tutorial/ess
ential/exceptions/index.html

More Related Content

What's hot (13)

PDF
javaexceptions
Arjun Shanka
 
PPTX
Introduction to java exceptions
Sujit Kumar
 
PPTX
Presentation on-exception-handling
Nahian Ahmed
 
ODP
Exception Handling In Java 15734
madhurendra pandey
 
PPT
Exception handling
Karthik Sekar
 
PPTX
Using pmd to ensure apex coding best practices
Ahmed Keshk
 
PPT
Java: Exception
Tareq Hasan
 
PPT
Exception handling
Iblesoft
 
PPTX
Chapter 13 exceptional handling
Praveen M Jigajinni
 
ODP
Exception Handling In Java
parag
 
PPTX
Exception handling
Abhishek Pachisia
 
PPT
Exception Handling Java
ankitgarg_er
 
PPT
Exception handling
M Vishnuvardhan Reddy
 
javaexceptions
Arjun Shanka
 
Introduction to java exceptions
Sujit Kumar
 
Presentation on-exception-handling
Nahian Ahmed
 
Exception Handling In Java 15734
madhurendra pandey
 
Exception handling
Karthik Sekar
 
Using pmd to ensure apex coding best practices
Ahmed Keshk
 
Java: Exception
Tareq Hasan
 
Exception handling
Iblesoft
 
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Exception Handling In Java
parag
 
Exception handling
Abhishek Pachisia
 
Exception Handling Java
ankitgarg_er
 
Exception handling
M Vishnuvardhan Reddy
 

Similar to Exception handling (20)

PPTX
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
PPTX
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
 
PPTX
Lecture 1 Try Throw Catch.pptx
VishuSaini22
 
PPT
Unit 5 Java
arnold 7490
 
PPT
Week7 exception handling
Alpesh Oza
 
PPT
Week7 exception handling
Alpesh Oza
 
PPT
Week7 exception handling
Alpesh Oza
 
PPTX
Exception handling chapter15
Kumar
 
PPT
F6dc1 session6 c++
Mukund Trivedi
 
PDF
Exception Handling.pdf of cjfbduifhwieuJN
241475
 
PPTX
SUBHASH.pptx
YashvanthGowda3
 
PPTX
6-Error Handling.pptx
amiralicomsats3
 
PPT
Exception handling
zindadili
 
PPTX
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
 
PPT
Exception and Error Handling in C++ - A detailed Presentation
debasisdas225831
 
PPT
Week7_ExceptionHandling.ppt
SatyaKumari18
 
PPTX
Exception Handling
Ferdin Joe John Joseph PhD
 
PDF
Exceptions and Exception Handling in C++
IRJET Journal
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
ZachariahAbera
 
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
 
Lecture 1 Try Throw Catch.pptx
VishuSaini22
 
Unit 5 Java
arnold 7490
 
Week7 exception handling
Alpesh Oza
 
Week7 exception handling
Alpesh Oza
 
Week7 exception handling
Alpesh Oza
 
Exception handling chapter15
Kumar
 
F6dc1 session6 c++
Mukund Trivedi
 
Exception Handling.pdf of cjfbduifhwieuJN
241475
 
SUBHASH.pptx
YashvanthGowda3
 
6-Error Handling.pptx
amiralicomsats3
 
Exception handling
zindadili
 
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
 
Exception and Error Handling in C++ - A detailed Presentation
debasisdas225831
 
Week7_ExceptionHandling.ppt
SatyaKumari18
 
Exception Handling
Ferdin Joe John Joseph PhD
 
Exceptions and Exception Handling in C++
IRJET Journal
 
Exception handling c++
Jayant Dalvi
 
Chapter_4_WP_with_C#_Exception_Handling_student_1.0.pptx
ZachariahAbera
 
Ad

More from abdullah619 (12)

PPT
Testing chapter updated (1)
abdullah619
 
PPTX
Queue
abdullah619
 
PPTX
Stacks
abdullah619
 
PPTX
Array list(1)
abdullah619
 
PPTX
Lists
abdullah619
 
PPTX
Collections (1)
abdullah619
 
PPTX
Polumorphism
abdullah619
 
PPTX
Abstrcation
abdullah619
 
PPTX
4 oo inheritance in c# (1)
abdullah619
 
PPTX
3 instantiating an object in c# (1)
abdullah619
 
PPTX
Methods in c# (1)
abdullah619
 
PPTX
5. linked list
abdullah619
 
Testing chapter updated (1)
abdullah619
 
Stacks
abdullah619
 
Array list(1)
abdullah619
 
Lists
abdullah619
 
Collections (1)
abdullah619
 
Polumorphism
abdullah619
 
Abstrcation
abdullah619
 
4 oo inheritance in c# (1)
abdullah619
 
3 instantiating an object in c# (1)
abdullah619
 
Methods in c# (1)
abdullah619
 
5. linked list
abdullah619
 
Ad

Recently uploaded (20)

PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
PDF
Dealing with JSON in the relational world
Andres Almiray
 
PPTX
Cubase Pro Crack 2025 – Free Download Full Version with Activation Key
HyperPc soft
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
PDF
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
Dealing with JSON in the relational world
Andres Almiray
 
Cubase Pro Crack 2025 – Free Download Full Version with Activation Key
HyperPc soft
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
WYSIWYG Web Builder Crack 2025 – Free Download Full Version with License Key
HyperPc soft
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Human Resources Information System (HRIS)
Amity University, Patna
 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 

Exception handling

  • 1. CIS 2403- Object Oriented Programming 1 Exception Handling CIS 2403 Object Oriented Programming
  • 2. CIS 2403- Object Oriented Programming 2 Understanding Exceptions • An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. • Exceptions are caused by errors in program logic or insufficient system resources.
  • 3. CIS 2403- Object Oriented Programming 3 Example
  • 4. CIS 2403- Object Oriented Programming 4 Understanding Exceptions • The programs you write can generate many types of potential exceptions, including when:  Your program asks for user input, but the user enters invalid data.  The program attempts to divide an integer by zero.  You attempt to access an array with a subscript that is too large or too small.  You calculate a value that is too large for the answer’s variable type.
  • 5. CIS 2403- Object Oriented Programming 5 Understanding Exceptions • These errors are called exceptions because presumably they are not usual occurrences; they are “exceptional.” • The object-oriented techniques used to manage such errors make up the group of methods known as exception handling. If you do not handle an exception, the running program terminates abruptly.
  • 6. CIS 2403- Object Oriented Programming 6 Traditional Error Handling • Programmers had to deal with error conditions long before object-oriented methods were conceived • This code successfully prevents division by zero, but it does not really “handle an exception” because no Exception class object is created.
  • 7. CIS 2403- Object Oriented Programming 7 Traditional Error Handling • If a user enters 0 for gallonsOfGas in only one case out of 1,000, then the if statement is executed unnecessarily 999 times! • In that case, it is more efficient to eliminate the if test and instantiate an Exception object when needed.
  • 8. CIS 2403- Object Oriented Programming 8 Handling Exceptions • A program can catch exceptions by using a combination of the try, catch, and finally blocks.  The try block identifies a block of code in which an exception can occur.  The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception.  The finally block identifies a block of code that is guaranteed to execute.
  • 9. CIS 2403- Object Oriented Programming 9 The Try Block • The first step is to enclose the code that might throw an exception within a try block. • To associate an exception handler with a try block, you must put a catch block after it
  • 10. CIS 2403- Object Oriented Programming 10 The catch Blocks • You associate exception handlers with a try block by providing one or more catch blocks directly after the try block. • The catch block contains code that is executed if and when the exception handler is invoked.
  • 11. CIS 2403- Object Oriented Programming 11 The finally Block • The finally block always executes when the try block exits.
  • 12. CIS 2403- Object Oriented Programming 12 Let’s Apply
  • 13. CIS 2403- Object Oriented Programming 13 Resources • Farrell.. Microsoft® Visual C# 2012: An Introduction to Object-Oriented Programming, International Edition, 5th Edition. Cengage Learning UK, 2013. • Oracle, Java Documentation: https://docs.oracle.com/javase/tutorial/ess ential/exceptions/index.html