-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig16_03.cpp
39 lines (36 loc) · 1 KB
/
fig16_03.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Fig 16.3:
// Demonstrating exception throwing.
#include <iostream>
#include <exception>
// throw, catch, and rethrow exception
void throwException()
{
// try exception and catch it immediately
try
{
std::cout << " Function throwException throws an exception\n";
throw exception();
} // end try
catch (exception& ) // handle exception
{
std::cout << " Exception handled in function throwException"
<< "\n Function throwException rethrows exception";
throw; // rethrow exception for further processing
} // end catch
std::cout << "This also should not print \n";
} // end function throwException
int main (void)
{
// throw exception
try
{
std::cout << "\nmain invokes function throwException\n";
throwFunction();
std::cout << "This should not print\n";
} // end try
catch (exception & ) // handle exception
{
std::cout << "\n\nException handled in main\n";
} // end catch
std::cout << "Program control continues after catch in main \n\n";
} // end