-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.cpp
More file actions
85 lines (73 loc) · 1.41 KB
/
Copy pathtest2.cpp
File metadata and controls
85 lines (73 loc) · 1.41 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <tuple>
#include <random>
using namespace std;
class CustomisedException : public exception {
public:
CustomisedException(string message, int line=0, string file="") {
message_ = message;
line_ = line;
file_ = file;
::exception();
}
virtual const char* what() const throw()
{
stringstream message;
message << message_ << " ";
if ( line_ && file_ != "" )
message << "Error at line " << line_ << " in " << file_;
return message.str().c_str();
}
~CustomisedException() throw () {};
private:
string message_;
int line_;
string file_;
};
class myexception: public exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
};
int main(int argc, char *argv[])
{
try
{
if (argc < 2) {
throw CustomisedException("Too few arguments.", __LINE__, __FILE__);
}
}
catch (CustomisedException& e)
{
cerr << "exception caught: " << e.what() << endl;
}
try
{
if (1 != 2) {
throw myexception();
}
}
catch (exception& e)
{
cerr << "exception caught: " << e.what() << endl;
}
try
{
if (1 != 2) {
throw CustomisedException("Last one.");
}
}
catch (exception& e)
{
cerr << "exception caught: " << e.what() << endl;
}
return 0;
}