Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第3章 例外 と期待

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

短いプログラムを書けるようになり、入力に関する潜在的な問題について考え始めた。成功か否かを示すbool値を返す関数の書き方を示したが、問題を扱うより洗練された方法がある。この章では例外と期待という二つのアプローチを説明する。C++の他の要素を学び、関数の書き方の練習を重ね、ブロックスコープの理解を深めることになる。

C++は長い間例外をサポートしてきたが、組み込みコードを書く人の中にはオーバーヘッドがあるため使用を避ける者もいる。 C++23では、戻り値かエラーのいずれかを保持する新しい型であるstd::expectedが導入された。この章では両方のアプローチを示し、第4章でより大きなプログラムを構築する準備を整える。

例外

ほとんどの言語は、問題が発生した際に例外を発生させる方法と、その状況を処理する手段を提供している。 他のプログラミング言語と同様、例外を発生させる文を実行すると、プログラムは別の場所にジャンプする。 何か問題が起きた時に関数から抜け出したり別の場所にジャンプできるのは、魔法のように思えるかもしれない。 例外処理の具体的な仕組みはツールチェーンによって異なるが、別の場所にジャンプするには追加の処理が必要だ。 関数をnoexceptとして宣言すれば、例外を投げないと宣言できる。これによりツールチェーンは一部のオーバーヘッドを回避できる。 noexceptの具体例は第11章で後述する。

例外を検知したら、その対処法も決めなければならない。何もしないで問題を外界に報告するのが最善の場合もある。 第2章では数値を取得する関数を作成し、mainから呼び出した:

const bool OK = get_number(std::cin, number);

falseが返った場合、コードは単に「何か問題が起きた」と表示しただけだ。 第4章では複数の数値を取得しようとするが、問題が発生したらどうするか? プログラム全体を停止させることもできるが、そうするとユーザは全てを再入力しなければならない。 データがネットワーク経由やファイル、他のプロセスから来ている場合、停止してエラーを報告するのは許容範囲かもしれない。 その後、根本的な問題を修正してデータを再送信できる。 エラー処理は常に、boolを返すか例外をスローするか別の方法を選ぶか、選択肢が分かれる。 どう対応するかは通常、コンテキスト次第だ。

この章では単一の数値を入力するプログラムに焦点を当てよう。次の章では複数の数値を取得・保存する方法を学ぶ。 このセクションでは問題の発生を例外で示す。次のセクションでは別の手法を学ぶ。

例外とは、実行時に問題が発生したことを示すイベントであり、プログラムの通常の実行や流れを停止させる。の例外には三つの要素がある。第一に、コードが例外をスローできることだ図3-1に示すように、ボールを投げるようなものと考えられる。問題を示すために投げるのだ。

Illustration of a stick figure throwing a ball, representing the concept of throwing an exception in programming.
図3-1. 例外の投げ方

次に、が推測した通り、呼び出し側が例外をキャッチできる。第三に、呼び出し側が関数が例外をスローする可能性があることを知っていれば、そのコードを呼び ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653