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

Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
Vasavi College of Engg
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
farhan amjad
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 

What's hot (20)

Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Exception handling
Exception handlingException handling
Exception handling
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Parsing
ParsingParsing
Parsing
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
File in C language
File in C languageFile in C language
File in C language
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 

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

Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
Sujit Kumar
 
exceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.pptexceptionvdffhhhccvvvv-handling-in-java.ppt
exceptionvdffhhhccvvvv-handling-in-java.ppt
yjrtytyuu
 
Chelberg ptcuser 2010
Chelberg ptcuser 2010Chelberg ptcuser 2010
Chelberg ptcuser 2010
Clay Helberg
 
Design byexceptions
Design byexceptionsDesign byexceptions
Design byexceptions
Asif Tasleem
 
Errors and exception
Errors and exceptionErrors and exception
Errors and exception
Rishav Upreti
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 

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

Top Management of 5 recognized comapanies
Top Management of 5 recognized comapaniesTop Management of 5 recognized comapanies
Top Management of 5 recognized comapanies
Abhishek Pachisia
 
Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)Program Evaluation and Review Technique (PERT)
Program Evaluation and Review Technique (PERT)
Abhishek Pachisia
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
Abhishek Pachisia
 

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

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

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