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

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-handling
Nahian Ahmed
 
Exception Handling In Java 15734
Exception Handling In Java 15734Exception Handling In Java 15734
Exception Handling In Java 15734
madhurendra pandey
 
Exception handling
Exception handlingException handling
Exception handling
Karthik Sekar
 
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
Ahmed Keshk
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
Tareq Hasan
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
parag
 
Exception handling
Exception handlingException handling
Exception handling
Abhishek Pachisia
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
ankitgarg_er
 
Exception handling
Exception handling Exception handling
Exception handling
M Vishnuvardhan Reddy
 

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.pptx
VishuSaini22
 
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
sunilsoni446112
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
imran khan
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
1606125427-week8.pptx
1606125427-week8.pptx1606125427-week8.pptx
1606125427-week8.pptx
SumbalIlyas1
 
Exception handling
Exception handlingException handling
Exception handling
pooja 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 Handling
MaqdamYasir
 
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
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptx
ApurbaSamanta9
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling
Exception handlingException handling
Exception handling
zindadili
 
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
Bruno Henrique Rother
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptionsAsif Tasleem
 
Anti patterns part 2
Anti patterns part 2Anti patterns part 2
Anti patterns part 2
Return on Intelligence
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
raksharao
 

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

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)
abdullah619
 
Queue
QueueQueue
Stacks
StacksStacks
Stacks
abdullah619
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
abdullah619
 
Lists
Lists Lists
Lists
abdullah619
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
abdullah619
 
Polumorphism
PolumorphismPolumorphism
Polumorphism
abdullah619
 
Abstrcation
AbstrcationAbstrcation
Abstrcation
abdullah619
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)
abdullah619
 
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)
abdullah619
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)
abdullah619
 
5. linked list
5. linked list5. linked list
5. linked list
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

A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 

Recently uploaded (20)

A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 

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