Skip to Content
『Learning Python 第6版』
book

『Learning Python 第6版』

by Mark Lutz
March 2025
Intermediate to advanced
1172 pages
24h 57m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Python 第6版』

第34章. 例外コーディングの詳細

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

前の章では例外関連のin文をざっと見た。この章では、Pythonの例外処理構文をより詳しく説明する。具体的には、try 、raise 、assert 、with 文の背後にある詳細を探る。これらの文はほとんど単純だが、Pythonコードで例外条件を扱うための強力なツールを提供していることが発見できるだろう。

トライ宣言

まず、try ステートメントは、コードが例外をキャッチする方法である。要するに、この文のメインブロックの実行中に例外が発生すると、プログラムはジャンプバックして文のハンドラの1つを実行し、そこから処理を続ける。そのハンドラは、try の中に入れ子になったexcept 、else 、finally 、except* 節で指定することができる。これらの節の構文とその有効な組み合わせには、別の規則が適用される。

これは表面的には単純なモデルであるが、try ステートメントのハンドラ節にはバラバラの目的があり、有効な組み合わせの規則があるため、それぞれ異なる味わいがある。このため、try'の共通の役割をまず単独で調べ、後でそれらの断片を組み合わせたステートメントとしてまとめることで、このテーマにアプローチすることにする。これは、try が Python のおぼろげな過去には別々の文であったという事実と類似しているが、ここでの焦点はその統一された現在である。

技術的にはtry の一部であるが、except* 節も次の章までお預けである。その理由は、その章の例外オブジェクトのトピックを侵すということもあるが、実際にはほとんど役に立たない拡張機能のために、try の話を大幅に複雑にしてしまうツールだからである。ここでの優先事項は基礎を学ぶことである。

try ステートメント句

try ステートメントを記述するとき、try ヘッダの後や下にさまざまな節を記述することができる。表34-1は、参照とプレビューの両方として、すべての可能な形式をまとめたものである。except 節が例外をキャッチし、finally 節が途中で実行されることはすでに見た。else また、except* 節は例外グループを処理し、emptyを除くすべてのexcept 形式をサポートする。

正式には、try は、表34-1の節の少なくとも1つを使用しなければならない。except 節はいくつあってもよいが、else をコーディングできるのは、except が少なくとも1つある場合だけであり、else とfinally は1つずつしかない。finally は、except およびelse と同じ文に記述することができる。順序の規則はこの章の後半で説明する。

表34-1 try ステートメント句とフォーム
条項形式 インタプリタ
except: すべての(または他のすべての)例外タイプをキャッチする
except name: 特定の例外のみをキャッチする
except name as var: リストされた例外をキャッチし、そのインスタンスを代入する。
except (name1, name2): タプルにリストされた例外のいずれかをキャッチする。
except (name1, name2) as var: リストされた例外をキャッチし、そのインスタンスを代入する。
else: ...
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

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Jeroen Janssens, Thijs Nieuwdorp, 中田 秀基
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 9798341634367