Two Questions on Exception Handling

Software: Dev C++
Problem 1(Line 8):
This example is to show we can use exception (and its derived class) to create exception objects that will be thrown by the program(maybe inaccurate description here)
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
#include<iostream>
#include<exception>
using namespace std;

void divide(int i, int j)  
{
if (j == 0)       
throw exception("\n...Divide by 0 happens...");
else
cout << i << '/' << j << " = " << i/j << "..." << i%j;
}

int main()                
{
int i,j;
cout << "This program calcuates the quotient and remainder, "
   << "Please enter a number and divisor";
cin >> i >> j;

try {
divide (i,j);
}
catch (exception e) {
cerr << e.what();
}
}


Error message:
no matching function for call to `std::exception::exception(const char[24])
Thank you. I will post another question later.
The standard std::exception does not take a string argument, even though some implementations do (e.g. Microsoft's implmentations).

std::exception is light because it's a base class other stuff in the standard library. You probably need std::logic_error or std::runtime_error defined in stdexcept.
http://www.aoc.nrao.edu/~tjuerges/ALMA/STL/html/classstd_1_1exception.html
closed account (z05DSL3A)
I would go for an invalid_argument[1] exception.

[1] http://www.cplusplus.com/reference/std/stdexcept/invalid_argument.html
Quite right.
Thank you. kbw, Grey Wolf. I use logic_error and the problem is solved!
Topic archived. No new replies allowed.