0% found this document useful (0 votes)
23 views27 pages

Exceptions in C++

Uploaded by

venomzeus79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views27 pages

Exceptions in C++

Uploaded by

venomzeus79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Exceptions in C++

• Types of Errors
– Syntax
– Logical
– Run-time
• Exceptions are errors that occur at run time
– Running out of memory
– Not being able to open a file
– Out of bounds index to a vector
– Unable to read file content
– Unable to connect to data base/network
– ……………………….
Exceptions
• Exceptions: Are errors that occur at run-time
• Exception handling: A systematic, object oriented approach
to handling errors generated by C++ classes.
• History (method used in past) of managing/handling run-
time errors
– If(somefun() == ERROR_RET_VAL)
// handle the error of call error-handler function
else
//proceed normally
If(anotherfun() == NULL)
// handle the error of call error-handler function
else
//proceed normally
Drawback of this approach
Every Call to function must be examined by the program

Surrounding each function call with if-else statements and adding statements
To handle error or call an error handle routine

Require a lot of code for error management. Error handling code and
application logic is mixed

The problem becomes more complex when classes are used, since errors may
takes place without a function being explicitly called
Ex: SomeClass obj1, obj2, obj3……….
The constructors are called implicitly, there is no return value to be checked

The run time error management is complicated, when an application uses


class libraries
Exception Mechanism
Exception Syntax
• The exception mechanism uses three C++ key words:
– try, catch, throw
• Create a new entity called exception class
Example: division by zero (integer division)
• Write a C++ program to read two integer numbers and
find the division of the two.
Example: create a class to describe run time error
Example: division by zero (integer division)
Example:
• Write a C++ program to create function
int getQuotient() which reads two integer numbers
and returns the division of two numbers. Identify
exceptions and manage using try, catch and throw
keywords of C++.
[use the class DivisionByZeroException]
Example: division by zero (integer division)
Example: division by zero (integer division)
Example: division by zero (integer division)
Example: division by zero (integer division)
• Redefine the previous program to define the function
in a separate class.
Example: division by zero (integer division)
Example: division by zero (integer division)
Example: division by zero (integer division)
Example
• Consider a Car object defined by Car(carSpeed,
maxSpeed, transmissionType(auto/manual)). The
driving of a car is defined by a function
raceCar()/driveCar() and raceCar(speed)/
driveCar(speed). The car cannot be drive beyond
maximum speed, the program has to raise exception if
it reaches maximum speed and reset to different speed
less than max speed or zero speed. The application has
to print car details.
Example: division by zero (integer division)
Example: division by zero (integer division)
Example: division by zero (integer division)
Example: division by zero (integer division)
When to use exception handling
• Exception handling is designed to process
synchronous errors:
– out_of_range array subscripts
– Arithmetic overflow
– Invalid function parameters
– Unsuccessful memory allocation (due to lack of
memory)
• Exception handling is not designed to process
errors associated with asynchronous events: disk
i/o completions, network message arrivals, mouse
click and keystorkes
• Reference: C++ How to program, Dietel
Steps: exception handling
• Defining an exception class to represent the type of the
problem that might occur
• Enclosing code in a try block
• Defining a catch handler to process a exception
• Terminating model of exception handling
– Termination model of exception handling
– Resumption model of exception handling

• Reference: C++ How to program, Dietel


Library classes for exception handling
Library classes for exception handling
• The ‘exception’ [defined in header: <exception>] class
is the base class for all the exceptions, contains ‘
virtual what()’ function , which derived classes can
override to issue appropriate error messages.
• the runtime_error and logic-error are immediate sub
classes of exception.
• The exceptions thrown by operators are
– bad_alloc
– bad_cast
– bad_type_id
– bad_exception
– [Note: Read yourself and illustrate with example]
Library classes for exception handling
• logic-error
– invalid_argument
– length_error
– out_of_range
• runtime_error (arithmetic overflow errors)
– overflow_error
– underflow_error
Library classes for exception handling
• The example are given in word document
• The case study: Bank application.
– Define some exceptions conditions
– Manage using: try, catch, throw

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy