Skip to content

Commit 2ed465d

Browse files
committed
第2版のサンプルコードを追加
1 parent fe0b53d commit 2ed465d

File tree

256 files changed

+6933
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+6933
-1
lines changed

2nd/003_exception/001_about.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <stdexcept>
7+
8+
using namespace std;
9+
10+
void g() {
11+
throw runtime_error("エラーが発生した!");
12+
}
13+
14+
void f() {
15+
try {
16+
g();
17+
}
18+
catch (runtime_error& e) { // 関数f()で送出された例外を捕捉
19+
cerr << e.what() << endl; // エラーメッセージを取得
20+
throw; // 捕捉した例外を再送出
21+
}
22+
// exceptionクラスを継承したすべての例外オブジェクトを捕捉
23+
catch (exception& e) {
24+
cerr << "exceptionを継承した例外オブジェクトが送出された" << endl;
25+
}
26+
// あらゆる例外を捕捉
27+
catch (...) {
28+
cerr << "何かの例外オブジェクトが送出された" << endl;
29+
}
30+
}
31+
32+
int main() {
33+
f();
34+
}
35+

2nd/003_exception/002_exception.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <stdexcept>
7+
8+
using namespace std;
9+
10+
void f(int i) {
11+
const int size = 3;
12+
int ar[size];
13+
14+
if (i < 0 || i >= size) {
15+
throw out_of_range("範囲外の値が渡されました");
16+
}
17+
}
18+
19+
int main() {
20+
try {
21+
f(5);
22+
}
23+
catch (out_of_range& e) {
24+
const char* message = e.what();
25+
cerr << message << endl;
26+
// throw;
27+
}
28+
}

2nd/003_exception/003_noexcept.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
class Item {
6+
int id = 0;
7+
public:
8+
int getId() const noexcept {
9+
return id;
10+
}
11+
};
12+
13+
int getItemId(const Item& item)
14+
noexcept(noexcept(item.getId())) {
15+
return item.getId();
16+
}
17+
18+
int main() {
19+
Item item;
20+
{
21+
int id = item.getId();
22+
}
23+
{
24+
int id = getItemId(item);
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2012-2015
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <string>
7+
#include <system_error>
8+
using namespace std;
9+
10+
// サーバに接続を試みるOSのAPI
11+
int connect_to_server_api()
12+
{
13+
return 503;
14+
}
15+
16+
int main()
17+
{
18+
try {
19+
if (connect_to_server_api() == 503) {
20+
// ネットワークがダウンしている。
21+
// 503は「サービスが利用できない」ことを
22+
// 表すHTTPのステータスコード。
23+
error_code ec = make_error_code(errc::network_down);
24+
throw system_error(ec);
25+
}
26+
}
27+
catch (system_error& e) {
28+
error_code ec = e.code();
29+
cout << ec.message() << endl;
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <stdexcept>
7+
#include <exception>
8+
9+
using namespace std;
10+
11+
void print(exception_ptr e) {
12+
if (e == nullptr) {
13+
cout << "no exception" << endl;
14+
}
15+
else {
16+
cerr << "some exceptions" << endl;
17+
}
18+
}
19+
20+
int main() {
21+
exception_ptr err1;
22+
print(err1);
23+
try {
24+
exception_ptr err2;
25+
print(err2);
26+
throw runtime_error("test for exception_ptr");
27+
}
28+
catch (...) {
29+
exception_ptr err3 = current_exception();
30+
print(err3);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <stdexcept>
7+
#include <exception>
8+
9+
using namespace std;
10+
11+
void print(exception_ptr e) {
12+
if (e == nullptr) {
13+
cout << "no exception" << endl;
14+
}
15+
else {
16+
cerr << "some exceptions" << endl;
17+
}
18+
}
19+
20+
int main() {
21+
exception_ptr err1 = current_exception();
22+
print(err1);
23+
try {
24+
exception_ptr err2 = current_exception();
25+
print(err2);
26+
throw runtime_error("test for current_exception()");
27+
}
28+
catch (...) {
29+
exception_ptr err3 = current_exception();
30+
print(err3);
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <exception>
7+
8+
using namespace std;
9+
10+
class xxx : public exception { };
11+
class yyy : public exception { };
12+
13+
void throw_and_print(exception_ptr e) {
14+
try {
15+
rethrow_exception(e);
16+
}
17+
catch (xxx&) {
18+
cerr << "xxx exception" << endl;
19+
}
20+
catch (yyy&) {
21+
cerr << "yyy exception" << endl;
22+
}
23+
}
24+
25+
int main() {
26+
exception_ptr err1 = make_exception_ptr(xxx());
27+
throw_and_print(err1);
28+
try {
29+
throw yyy();
30+
}
31+
catch (...) {
32+
exception_ptr err2 = current_exception();
33+
throw_and_print(err2);
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <stdexcept>
7+
#include <exception>
8+
9+
using namespace std;
10+
11+
class xxx : public exception { };
12+
13+
int main() {
14+
exception_ptr err = make_exception_ptr(xxx());
15+
try {
16+
rethrow_exception(err);
17+
}
18+
catch (xxx&) {
19+
cerr << "xxx exception" << endl;
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <stdexcept>
7+
#include <exception>
8+
9+
using namespace std;
10+
11+
void print(exception_ptr e) {
12+
if (e == nullptr) {
13+
cout << "no nested exception" << endl;
14+
}
15+
else {
16+
cerr << "some nested exception" << endl;
17+
}
18+
}
19+
20+
int main() {
21+
nested_exception err1;
22+
print(err1.nested_ptr());
23+
try {
24+
throw runtime_error("test for nested_exception()");
25+
}
26+
catch (...) {
27+
nested_exception err2;
28+
print(err2.nested_ptr());
29+
}
30+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2012-2013
2+
// Akira Takahashi, Toshihiko Ando, Kohsuke Yuasa,
3+
// Yusuke Ichinohe, Masaya Kusuda, wraith.
4+
// Released under the CC0 1.0 Universal license.
5+
#include <iostream>
6+
#include <exception>
7+
8+
using namespace std;
9+
10+
class xxx : public exception { };
11+
class yyy : public exception { };
12+
13+
int main() {
14+
try {
15+
try {
16+
try {
17+
throw xxx();
18+
}
19+
catch (...) {
20+
throw nested_exception();
21+
}
22+
}
23+
catch (nested_exception& n) {
24+
n.rethrow_nested();
25+
}
26+
}
27+
catch (xxx&) {
28+
cerr << "xxx exception" << endl;
29+
}
30+
31+
try {
32+
try {
33+
try {
34+
throw xxx();
35+
}
36+
catch (...) {
37+
throw_with_nested(yyy());
38+
}
39+
}
40+
catch (yyy& e) {
41+
rethrow_if_nested(e);
42+
cerr << "no nested_exception" << endl;
43+
}
44+
}
45+
catch (xxx&) {
46+
cerr << "xxx exception" << endl;
47+
}
48+
49+
try {
50+
try {
51+
throw yyy();
52+
}
53+
catch (yyy& e) {
54+
rethrow_if_nested(e);
55+
cerr << "no nested_exception" << endl;
56+
}
57+
}
58+
catch (yyy&) {
59+
cerr << "yyy exception" << endl;
60+
}
61+
}

0 commit comments

Comments
 (0)