SlideShare a Scribd company logo
1 of 12
Abhishek Pachisia
B-Tech(IT)-II yr
09SETIT144
Exceptions
  – Indicate problems that occur during a program’s
    execution
  – Occur infrequently
Exception handling
  – Can resolve exceptions
     • Allow a program to continue executing or
     • Notify the user of the problem and
     • Terminate the program in a controlled manner
  – Makes programs robust and fault-tolerant
• An exception is a condition that cannot be resolved within the context of the
  operation that caused it. (examples?)
• To throw an exception is to signal that such a condition has occurred.
• To catch an exception is to transfer control to an exception handling routine,
  altering the normal execution flow of the program.
• Because exceptions are outside the normal operation of a program the normal,
  default action is to write out an error message and terminate the offending
  process.
• Older languages did not provide for exception handling as part of the
  language definition (Fortran, COBOL, C).
• Newer languages have added language constructs to facilitate exception
  handling (Ada, C++, Java).
• Most operating systems provide some sort of signal mechanism that can be
  used to provide a form of exception handling.
Exceptions can occur at many levels:

 Hardware/operating system level.
   • Arithmetic exceptions; divide by 0.
   • Memory access violations; stack over/underflow.
 Language level.
   • Type conversion; illegal values, improper casts.
   • Bounds violations; illegal array indices.
   • Bad references; null pointers.
 Program level.
   • User defined exceptions.
 Intermixing program and error-handling logic
    – Pseudo code outline
        Perform a task
        If the preceding task did not execute correctly
          Perform error processing
        Perform next task
        If the preceding task did not execute correctly
          Perform error processing
 Sometimes it is hard to distinguish an exception from normal
  processing.
 Makes the program difficult to read, modify, maintain and debug.
 Impacts performance.

Note:– In most large systems, code to handle errors and exceptions represents
“>80%” of the total code of the system.
Enclose code that may have an error in try block.
Follow with one or more catch blocks.
    Each catch block has an exception handler
If exception occurs and matches parameter in catch block, code in catch
block executed
If no exception thrown, exception handlers skipped and control resumes
after catch blocks
throw point - place where exception occurred
    Control cannot return to throw point
• Use assert
   – If assertion false, the program terminates

• Ignore exceptions
   – Use this "technique" on casual, personal programs - not commercial!

• Abort the program
   – Appropriate for nonfatal errors give appearance that program functioned
     correctly
   – Inappropriate for mission-critical programs, can cause resource leaks

• Set some error indicator
   – Program may not check indicator at all points there error could occur
•Test for the error condition
   − Issue an error message and call exit.
   − Pass error code to environment.
• setjump and longjump
   −In <csetjmp>
   −Jump out of deeply nested function calls back to an error handler.
   −Dangerous - unwinds the stack without calling destructors for automatic
   objects (more later).
•Specific errors
   −Some have dedicated capabilities for handling them
   −If new fails to allocate memory new handler function executes to deal
   with problem
throw - indicates an exception has occurred
  Usually has one operand (sometimes zero) of any type.
      −If operand an object, called an exception object.
      −Conditional expression can be thrown
  Code referenced in a try block can throw an exception.
  Exception caught by closest exception handler.
  Control exits current try block and goes to catch handler (if it
  exists).
  Exception not required to terminate program
     −However, terminates block where exception occurred
o Exception handlers are in catch blocks.
oCaught if argument type matches throw type.
         If not caught then terminate called which (by default) calls abort
oIf no handler matches thrown object
    •Searches next enclosing try block
    •If none found, terminate called
    •If found, control resumes after last catch block
    •If several handlers match thrown object, first one found is executed
ocatch parameter matches thrown object when
    •They are of the same type.
    • The catch parameter is a public base class of the thrown object.
    •The catch parameter is a base-class pointer/ reference type and the thrown
    object is a derived-class pointer/ reference type.
•The catch handler is catch( ... ).
   •Thrown const objects have const in the parameter type.
oUnreleased resources
   •Resources may have been allocated when exception thrown
   catch handler should delete space allocated by new and close any opened
   files.
ocatch handlers can throw exceptions
   •Exceptions can only be processed by outer try blocks.
oUsed when an exception handler cannot process an exception.
oRethrow exception with the statement:
    throw;
    •No arguments
    •If no exception thrown in first place, calls terminate
oHandler can always rethrow exception, even if it performed some
processing
oRethrown exception detected by next enclosing try block

More Related Content

What's hot

Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Object oriented testing
Object oriented testingObject oriented testing
Object oriented testingHaris Jamil
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVAKunal Singh
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Chapter 13 - I/O Systems
Chapter 13 - I/O SystemsChapter 13 - I/O Systems
Chapter 13 - I/O SystemsWayne Jones Jnr
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
 
Evolution of operating system
Evolution of operating systemEvolution of operating system
Evolution of operating systemArshad khan
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 

What's hot (20)

Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Object oriented testing
Object oriented testingObject oriented testing
Object oriented testing
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
White box ppt
White box pptWhite box ppt
White box ppt
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Chapter 13 - I/O Systems
Chapter 13 - I/O SystemsChapter 13 - I/O Systems
Chapter 13 - I/O Systems
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Evolution of operating system
Evolution of operating systemEvolution of operating system
Evolution of operating system
 
Exception handling
Exception handlingException handling
Exception handling
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Debugging
DebuggingDebugging
Debugging
 
Java awt
Java awtJava awt
Java awt
 
Files in java
Files in javaFiles in java
Files in java
 

Viewers also liked

Viewers also liked (10)

encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
polymorphism
polymorphism polymorphism
polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Similar to Exception handling

Exception handling
Exception handlingException handling
Exception handlingpooja kumari
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2thenmozhip8
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.pptJAYESHRODGE
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptionsSujit Kumar
 
exceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptexceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptyjrtytyuu
 
Chelberg ptcuser 2010
Chelberg ptcuser 2010Chelberg ptcuser 2010
Chelberg ptcuser 2010Clay Helberg
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptxApurbaSamanta9
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptionsAsif Tasleem
 
Errors and exception
Errors and exceptionErrors and exception
Errors and exceptionRishav Upreti
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxVishuSaini22
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 
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
 

Similar to Exception handling (20)

Exception handling
Exception handlingException handling
Exception handling
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2exception-handling-in-java.ppt unit 2
exception-handling-in-java.ppt unit 2
 
exception-handling-in-java.ppt
exception-handling-in-java.pptexception-handling-in-java.ppt
exception-handling-in-java.ppt
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
 
exceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptexceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.ppt
 
Chelberg ptcuser 2010
Chelberg ptcuser 2010Chelberg ptcuser 2010
Chelberg ptcuser 2010
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptx
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptions
 
Errors and exception
Errors and exceptionErrors and exception
Errors and exception
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception hierarchy
Exception hierarchyException hierarchy
Exception hierarchy
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Exception
ExceptionException
Exception
 
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
 

More from Abhishek Pachisia (20)

Telecom Industry
Telecom IndustryTelecom Industry
Telecom Industry
 
Strategic alignment model (SAM)
Strategic alignment model (SAM)Strategic alignment model (SAM)
Strategic alignment model (SAM)
 
V.G. siddhartha
V.G. siddharthaV.G. siddhartha
V.G. siddhartha
 
Boeing- The Frontiers
Boeing- The FrontiersBoeing- The Frontiers
Boeing- The Frontiers
 
Top Management of 5 recognized comapanies
Top Management of 5 recognized comapaniesTop Management of 5 recognized comapanies
Top Management of 5 recognized comapanies
 
Fourier transform
Fourier transformFourier transform
Fourier transform
 
Inference engine
Inference engineInference engine
Inference engine
 
Work breakdown structure
Work breakdown structureWork breakdown structure
Work breakdown structure
 
IEEE 802.11
IEEE 802.11IEEE 802.11
IEEE 802.11
 
Cloud Service Models
Cloud Service ModelsCloud Service Models
Cloud Service Models
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Perl
PerlPerl
Perl
 
Rpc
RpcRpc
Rpc
 
Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)
 
Hydrogen energy
Hydrogen energyHydrogen energy
Hydrogen energy
 
Rms titanic
Rms titanicRms titanic
Rms titanic
 
Vb.net (loop structure)
Vb.net (loop structure)Vb.net (loop structure)
Vb.net (loop structure)
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Tree
TreeTree
Tree
 
File System Implementation
File System ImplementationFile System Implementation
File System Implementation
 

Recently uploaded

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 

Recently uploaded (20)

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 

Exception handling

  • 2. Exceptions – Indicate problems that occur during a program’s execution – Occur infrequently Exception handling – Can resolve exceptions • Allow a program to continue executing or • Notify the user of the problem and • Terminate the program in a controlled manner – Makes programs robust and fault-tolerant
  • 3. • An exception is a condition that cannot be resolved within the context of the operation that caused it. (examples?) • To throw an exception is to signal that such a condition has occurred. • To catch an exception is to transfer control to an exception handling routine, altering the normal execution flow of the program. • Because exceptions are outside the normal operation of a program the normal, default action is to write out an error message and terminate the offending process. • Older languages did not provide for exception handling as part of the language definition (Fortran, COBOL, C). • Newer languages have added language constructs to facilitate exception handling (Ada, C++, Java). • Most operating systems provide some sort of signal mechanism that can be used to provide a form of exception handling.
  • 4. Exceptions can occur at many levels:  Hardware/operating system level. • Arithmetic exceptions; divide by 0. • Memory access violations; stack over/underflow.  Language level. • Type conversion; illegal values, improper casts. • Bounds violations; illegal array indices. • Bad references; null pointers.  Program level. • User defined exceptions.
  • 5.  Intermixing program and error-handling logic – Pseudo code outline Perform a task If the preceding task did not execute correctly Perform error processing Perform next task If the preceding task did not execute correctly Perform error processing  Sometimes it is hard to distinguish an exception from normal processing.  Makes the program difficult to read, modify, maintain and debug.  Impacts performance. Note:– In most large systems, code to handle errors and exceptions represents “>80%” of the total code of the system.
  • 6. Enclose code that may have an error in try block. Follow with one or more catch blocks. Each catch block has an exception handler If exception occurs and matches parameter in catch block, code in catch block executed If no exception thrown, exception handlers skipped and control resumes after catch blocks throw point - place where exception occurred Control cannot return to throw point
  • 7. • Use assert – If assertion false, the program terminates • Ignore exceptions – Use this "technique" on casual, personal programs - not commercial! • Abort the program – Appropriate for nonfatal errors give appearance that program functioned correctly – Inappropriate for mission-critical programs, can cause resource leaks • Set some error indicator – Program may not check indicator at all points there error could occur
  • 8. •Test for the error condition − Issue an error message and call exit. − Pass error code to environment. • setjump and longjump −In <csetjmp> −Jump out of deeply nested function calls back to an error handler. −Dangerous - unwinds the stack without calling destructors for automatic objects (more later). •Specific errors −Some have dedicated capabilities for handling them −If new fails to allocate memory new handler function executes to deal with problem
  • 9. throw - indicates an exception has occurred Usually has one operand (sometimes zero) of any type. −If operand an object, called an exception object. −Conditional expression can be thrown Code referenced in a try block can throw an exception. Exception caught by closest exception handler. Control exits current try block and goes to catch handler (if it exists). Exception not required to terminate program −However, terminates block where exception occurred
  • 10. o Exception handlers are in catch blocks. oCaught if argument type matches throw type. If not caught then terminate called which (by default) calls abort oIf no handler matches thrown object •Searches next enclosing try block •If none found, terminate called •If found, control resumes after last catch block •If several handlers match thrown object, first one found is executed ocatch parameter matches thrown object when •They are of the same type. • The catch parameter is a public base class of the thrown object. •The catch parameter is a base-class pointer/ reference type and the thrown object is a derived-class pointer/ reference type.
  • 11. •The catch handler is catch( ... ). •Thrown const objects have const in the parameter type. oUnreleased resources •Resources may have been allocated when exception thrown catch handler should delete space allocated by new and close any opened files. ocatch handlers can throw exceptions •Exceptions can only be processed by outer try blocks.
  • 12. oUsed when an exception handler cannot process an exception. oRethrow exception with the statement: throw; •No arguments •If no exception thrown in first place, calls terminate oHandler can always rethrow exception, even if it performed some processing oRethrown exception detected by next enclosing try block