SlideShare a Scribd company logo
1 of 13
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

Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptionsSujit Kumar
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Exception Handling In Java 15734
Exception Handling In Java 15734Exception Handling In Java 15734
Exception Handling In Java 15734madhurendra pandey
 
Using pmd to ensure apex coding best practices
Using pmd to ensure apex coding best practicesUsing pmd to ensure apex coding best practices
Using pmd to ensure apex coding best practicesAhmed Keshk
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Javaparag
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Javaankitgarg_er
 

What's hot (13)

javaexceptions
javaexceptionsjavaexceptions
javaexceptions
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception Handling In Java 15734
Exception Handling In Java 15734Exception Handling In Java 15734
Exception Handling In Java 15734
 
Exception handling
Exception handlingException handling
Exception handling
 
Using pmd to ensure apex coding best practices
Using pmd to ensure apex coding best practicesUsing pmd to ensure apex coding best practices
Using pmd to ensure apex coding best practices
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Exception handling
Exception handlingException handling
Exception handling
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Exception handling
Exception handling Exception handling
Exception handling
 

Similar to Exception handling

Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxVishuSaini22
 
Lecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxLecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxsunilsoni446112
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
1606125427-week8.pptx
1606125427-week8.pptx1606125427-week8.pptx
1606125427-week8.pptxSumbalIlyas1
 
Exception handling
Exception handlingException handling
Exception handlingpooja kumari
 
Software security (vulnerabilities) and physical security
Software security (vulnerabilities) and physical securitySoftware security (vulnerabilities) and physical security
Software security (vulnerabilities) and physical securityNicholas Davis
 
Software Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical SecuritySoftware Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical SecurityNicholas Davis
 
Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...
Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...
Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...Zhen Huang
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptxApurbaSamanta9
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Circuit breakers - Using Spring-Boot + Hystrix + Dashboard + Retry
Circuit breakers - Using Spring-Boot + Hystrix + Dashboard + RetryCircuit breakers - Using Spring-Boot + Hystrix + Dashboard + Retry
Circuit breakers - Using Spring-Boot + Hystrix + Dashboard + RetryBruno Henrique Rother
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptionsAsif Tasleem
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handlingraksharao
 

Similar to Exception handling (20)

Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
Lecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxLecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptx
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Exception
ExceptionException
Exception
 
Exception handling
Exception handlingException handling
Exception handling
 
1606125427-week8.pptx
1606125427-week8.pptx1606125427-week8.pptx
1606125427-week8.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
Software security (vulnerabilities) and physical security
Software security (vulnerabilities) and physical securitySoftware security (vulnerabilities) and physical security
Software security (vulnerabilities) and physical security
 
Software Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical SecuritySoftware Security (Vulnerabilities) And Physical Security
Software Security (Vulnerabilities) And Physical Security
 
Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...
Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...
Talos: Neutralizing Vulnerabilities with Security Workarounds for Rapid Respo...
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
CS3391 -OOP -UNIT – III  NOTES FINAL.pdfCS3391 -OOP -UNIT – III  NOTES FINAL.pdf
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptx
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
Exception handling
Exception handlingException handling
Exception handling
 
Circuit breakers - Using Spring-Boot + Hystrix + Dashboard + Retry
Circuit breakers - Using Spring-Boot + Hystrix + Dashboard + RetryCircuit breakers - Using Spring-Boot + Hystrix + Dashboard + Retry
Circuit breakers - Using Spring-Boot + Hystrix + Dashboard + Retry
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptions
 
Anti patterns part 2
Anti patterns part 2Anti patterns part 2
Anti patterns part 2
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 

More from abdullah619

More from abdullah619 (12)

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)
 
Queue
QueueQueue
Queue
 
Stacks
StacksStacks
Stacks
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Lists
Lists Lists
Lists
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Polumorphism
PolumorphismPolumorphism
Polumorphism
 
Abstrcation
AbstrcationAbstrcation
Abstrcation
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)
 
3 instantiating an object in c# (1)
3  instantiating an object in c# (1)3  instantiating an object in c# (1)
3 instantiating an object in c# (1)
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)
 
5. linked list
5. linked list5. linked list
5. linked list
 

Recently uploaded

WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 

Recently uploaded (20)

WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 

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