-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivideByZeroException.cpp
More file actions
41 lines (35 loc) · 1.12 KB
/
Copy pathdivideByZeroException.cpp
File metadata and controls
41 lines (35 loc) · 1.12 KB
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
40
41
//************************************************************************
// Author: Rolando Carreon
// Date: apr 3 2025
// Language: C++
// Assignment: Demo
// Description: preform divison, using exception handling to handle
// divison by zero
//************************************************************************
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
// declare local variables
int numerator = 10;
int denominator = 0;
// try to perform the divison, throw an error if the denom is 0
try
{
// if the denominator is 0, throw a runtim error
if( denominator == 0 )
throw runtime_error("Divide by zero error.");
// calculate and display the result
cout << "Result is " << numerator / denominator << endl;
}
// catch a thrown runtime error and display the appropriate message
// e is industry standard short for exception
// i is index
// e is used in a catch
catch( runtime_error& e )
{
cout << "Exception caught: " << e.what() << endl;
}
return 0;
}