0% found this document useful (0 votes)
15 views17 pages

UNIT 03 - Lecture 28 - Exception Handling

The document discusses exception handling in C++. It explains what exceptions are and how try, catch, and throw are used to detect, handle, and indicate exceptions. It covers stack unwinding, declaring new exception classes, and the standard exception hierarchy. The key aspects of C++ exception handling philosophy and mechanisms are explained.

Uploaded by

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

UNIT 03 - Lecture 28 - Exception Handling

The document discusses exception handling in C++. It explains what exceptions are and how try, catch, and throw are used to detect, handle, and indicate exceptions. It covers stack unwinding, declaring new exception classes, and the standard exception hierarchy. The key aspects of C++ exception handling philosophy and mechanisms are explained.

Uploaded by

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

1

Course: Object Oriented Programming using C++ (22CAU04A)


 Programme (s) : BCA

 Class : I B.C.A ‘A’

 Semester : II

 Unit & Lecture : 3 & 28

 Facilitator : Prof. J. JELSTEEN


Topic to be Discussed

Exception Handling
Outline

• What exceptions are and when to use them


• Using try, catch and throw to detect, handle and
indicate exceptions, respectively
• To process uncaught and unexpected exceptions
• To declare new exception classes
• How stack unwinding enables exceptions not
caught in one scope to be caught in another scope
• To handle new failures
• To understand the standard exception hierarchy
Introduction
• 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
Exception Handling in C++

• A standard mechanism for processing errors :

• Especially important when working on a project with a


large team of programmers

• C++ exception handling is much like Java’s

• Java’s exception handling is much like C++


Fundamental Philosophy

• Mechanism for sending an exception signal up the call


stack - Regardless of intervening calls

• Note: there is a mechanism based on same philosophy in


C
• setjmp(), longjmp()
• See man pages
Traditional Exception Handling
• Intermixing program and error-handling logic
Pseudocode 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

• Makes the program difficult to read, modify, maintain


and debug
• Impacts performance
Fundamental Philosophy

• Remove error-handling code from the program


execution’s “main line”

• Programmers can handle any exceptions they choose


• All exceptions
• All exceptions of a certain type
• All exceptions of a group of related types
Fundamental Philosophy

• Programs can

• Recover from exceptions


• Hide exceptions
• Pass exceptions up the “chain of command”
• Ignore certain exceptions and let someone else
handle them
Fundamental Philosophy

• An exception is a class
• Usually derived from one of the system’s exception
base classes
• If an exceptional or error situation occurs, program
throws an object of that class
• Object crawls up the call stack

• A calling program can choose to catch exceptions of


certain classes
• Take action based on the exception object
30
31 // enable user to enter two integers to divide
32 while ( cin >> number1 >> number2 )
33 {
34 // try block contains code that might throw exception
35 // and code that should not execute if an exception occurs
36 try
37 {
38 result = quotient( number1, number2 );
39 cout << "The quotient is: " << result << endl;
40 } // end try
41
42 // exception handler handles a divide-by-zero exception
43 catch ( DivideByZeroException &divideByZeroException )
44 {
45 cout << "Exception occurred: "
46 << divideByZeroException.what() << endl;
47 } // end catch
48
49 cout << "\nEnter two integers (end-of-file to end): ";
50 } // end while
51
52 cout << endl;
53 return 0; // terminate normally
54 } // end main
try Blocks

• Keyword try followed by braces ({})

• Should enclose

• Statements that might cause exceptions


• Statements that should be skipped in case of an
exception
Software Engineering Observation

• Exceptions may surface

• through explicitly mentioned code in a try block,


• through calls to other functions and
• through deeply nested function calls initiated by code
in a try block.
Catch Handlers

• Immediately follow a try block


• One or more catch handlers for each try block
• Keyword catch
• Exception parameter enclosed in parentheses
• Represents the type of exception to process
• Can provide an optional parameter name to interact with
the caught exception object
• Executes if exception parameter type matches the exception
thrown in the try block
• Could be a base class of the thrown exception’s class
Catch Handlers
try {
// code to try
}
catch (exceptionClass1 &name1) {
// handle exceptions of exceptionClass1
}
catch (exceptionClass2 &name2) {
// handle exceptions of exceptionClass2
}
catch (exceptionClass3 &name3) {
// handle exceptions of exceptionClass3
}
...
/* code to execute if
no exception or
catch handler handled exception*/
Common Programming Errors
Syntax error to place code between a try block
and its corresponding catch handlers

Each catch handler can have only a single


parameter
• Specifying a comma-separated list of exception parameters
is a syntax error

• Logic error to catch the same type in two


different catch handlers following a single try
block
Next Session
Exception Handling

Thank You

You might also like