forked from YunWGui/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.cpp
More file actions
62 lines (52 loc) · 989 Bytes
/
exception.cpp
File metadata and controls
62 lines (52 loc) · 989 Bytes
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
// Copyright (c) 2020 by Chrono
//
// g++ exception.cpp -std=c++11 -o a.out;./a.out
// g++ exception.cpp -std=c++14 -o a.out;./a.out
// g++ exception.cpp -std=c++14 -I../common -o a.out;./a.out
#include <iostream>
#include <stdexcept>
using namespace std;
class my_exception : public std::runtime_error
{
public:
using this_type = my_exception;
using super_type = std::runtime_error;
public:
my_exception(const char* msg):
super_type(msg)
{}
my_exception() = default;
~my_exception() = default;
private:
int code = 0;
};
[[noreturn]]
void raise(const char* msg)
{
throw my_exception(msg);
//throw runtime_error(msg);
}
void case1()
try
{
raise("error occured");
}
catch(const exception& e)
{
cout << e.what() << endl;
}
void case2() noexcept
{
cout << "noexcept" << endl;
}
void case3() noexcept
{
throw "Oh My God";
}
int main()
{
case1();
case2();
//case3();
cout << "exception demo" << endl;
}