Unit 14: Introduction to Exception Handling - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Unit 14: Introduction to Exception Handling

Description:

Solution: catch the exception and show a user-friendly message instead. ... ShowMessage('Data module not instantiated'); catch (Exception &E) // catch all other ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 9
Provided by: Prefer413
Category:

less

Transcript and Presenter's Notes

Title: Unit 14: Introduction to Exception Handling


1
Unit 14 Introduction to Exception Handling
  • How to Handle Errors
  • Nested if statements
  • Use exceptions
  • Exceptions
  • try/catch block
  • Example

2
How to Handle Errors (1) Nested if statements
  • Preventing potential run-time errors
  • Bad parameters (e.g., null string)
  • Un-initialized variables and pointers
  • Division by zero
  • Resource limits, etc.
  • Result
  • Large percentage of deeply nested if code used to
    prevent errors from occurring.

3
How to Handle Errors (1) Nested if Problems
  • Clutter normal program logic and error handling
    logic not separated
  • if (DM ! NULL)
  • if (DM-gtValidData()) DM-gtqCust-gtPost()
  • else
  • ShowMessage("Data module not instantiated")
  • Details of the Error May be Lost
  • if (TimeToMin(StartTime) lt 0)
  • return -1

4
BCB Exceptions
  • CBuilder throws an exception when an error
    occurs by default, VCL-based applications shows
    the exception
  • BCB displays an exception that is difficult for
    users to understand.
  • Solution catch the exception and show a
    user-friendly message instead.
  • Reference BCB Help Contents Programming with
    CBuilder Exception Handling

5
BCB Exceptions Example
  • E.g., EAccessViolation occurs if data module not
    instantiated

6
How to Handle Errors (2) Using Exceptions
  • Separates the handling of errors at the end of
    the normal program logic.
  • try/catch block
  • The block of code within the try block is subject
    to exception handling
  • The closing catch identifies the class of
    exception to be handled
  • To catch multiple exception classes, you must
    catch the most specific one first (check the
    Exception hierarchy in BCB help)

7
How to Handle Errors (2) Using Exceptions
Example
  • try
  • if (DM-gtValidData()) DM-gtqCust-gtPost()
  • catch (EAccessViolation AccessViolation)
  • ShowMessage("Data module not instantiated")
  • catch (Exception E) // catch all other
    exceptions
  • ShowException(this, E) // BCB default

8
Your Turn
  • An error occurs when the file is not found.
  • How do you handle this exception?
Write a Comment
User Comments (0)
About PowerShow.com