Notebook
これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2025/08/15 15:56:28.294 (台灣標準時)
Python の try, except, else, finally の使い方
Python の try, except, else, finally の使い方を記録しておくでござる。
例えば、文字列を整数に変換することを考える。文字列が確実に数字であるな
らば問題ないのでござるが、文字列がどんな内容なのかわからない場合、スク
リプトの実行がそこで止まってしまうことになる場合があり得るでござる。
以下のようなスクリプトを考えるでござる。
#!/usr/pkg/bin/python3.9
#
# Time-stamp: <2022/06/11 10:04:00 (CST) daisuke>
#
# import sys module
import sys
if (len (sys.argv) != 2):
print ("ERROR: one argument must be given!")
sys.exit ()
# string to be processed
string = sys.argv[1]
# conversion from string into integer
integer = int (string)
# printing result
print ("result of the conversion: %s ==> %d" % (string, integer) )
上のスクリプトを実行してみると、以下のようになるでござる。数字を与えれ
ば、問題はないでござる。
% ./except_00.py 123
result of the conversion: 123 ==> 123
ただし、アルファベットを与えると、以下のようになってしまうでござる。
% ./except_00.py abc
Traceback (most recent call last):
File "/amd/ogikubo/root/volume1/nas0/daisuke/backup/daisuke/src/python3/except/./except_00.py", line 18, in
integer = int (string)
ValueError: invalid literal for int() with base 10: 'abc'
try, except, else, finally を使うと、プログラムがその場で終了してしま
うことを防ぐことができるでござる。以下のようなスクリプトを考えるでござ
る。
#!/usr/pkg/bin/python3.9
#
# Time-stamp: <2022/06/11 10:15:41 (CST) daisuke>
#
# import sys module
import sys
if (len (sys.argv) != 2):
print ("ERROR: one argument must be given!")
sys.exit ()
# string to be processed
string = sys.argv[1]
# conversion from string into integer
try:
integer = int (string)
except:
print ("ERROR: string '%s' cannot be converted into integer!" % string)
integer = None
else:
print ("string '%s' is successfully converted into integer." % string)
finally:
print ("trial to convert string into integer completed.")
# printing result
if (integer):
print ("result of the conversion: %s ==> %d" % (string, integer) )
else:
print ("conversion from string into integer failed.")
実行してみるでござる。
% ./except_01.py 123
string '123' is successfully converted into integer.
trial to convert string into integer completed.
result of the conversion: 123 ==> 123
% ./except_01.py abc
ERROR: string 'abc' cannot be converted into integer!
trial to convert string into integer completed.
conversion from string into integer failed.
try の部分に試したいことを書き、 except の部分に失敗したときの対処を書
き、 else の部分に成功したときの対処を書き、 finally の部分に最後に行っ
ておきたいことを書けばよいでござる。
- About this article:
- author: daisuke
- file: 20220611_0.html
- category: Computer___Python
- title: Python の try, except, else, finally の使い方
- mode: public
- last modified: 2022/06/11 10:19:03 (Taiwan Standard Time)
- html generated: 2025/08/15 15:56:28.294 (Taiwan Standard Time)
Frequently accessed files
- Misc___Taiwan/20240207_00.html
- 149 page views
- title: 台灣から台灣の外に EMS で荷物を発送する方法
- Misc___Taiwan/20240819_00.html
- 129 page views
- title: 住所から台灣の郵便番号を調べる方法
- Computer___NetBSD/20230119_00.html
- 123 page views
- title: NetBSD でバイナリーパッケージを利用する方法
- Computer___TeX/20231107_00.html
- 110 page views
- title: LaTeX での counter の利用方法について
- Computer___Network/20230516_00.html
- 104 page views
- title: OpenVPN 2.6 を使い VPN Gate に接続するときの注意点
- Misc___Taiwan/20240903_01.html
- 85 page views
- title: 台湾の郵便局で EMS を利用して荷物を海外に送る方法
- Computer___Network/20230726_00.html
- 78 page views
- title: git の SSL certificate problem の解決方法
- Computer___NetBSD/20250301_01.html
- 73 page views
- title: yt-dlp で YouTube の動画をダウンロードするときのこと
- Computer___Network/20241214_00.html
- 63 page views
- title: Google Colab で Python 3.12 を使う方法
- Computer___NetBSD/20240805_03.html
- 62 page views
- title: NetBSD 10 のインストール
- Computer___Network/20240130_00.html
- 59 page views
- title: Google Colaboratory で Python 3.12 を使う方法
- Computer___TeX/20240414_00.html
- 55 page views
- title: LuaTeX での fontspec を使ったフォントの設定の方法
- Food___Taiwan/20230609_06.html
- 55 page views
- title: 「越芳越南牛肉河粉」のフォー
- Computer___TeX/20230726_01.html
- 52 page views
- title: Beamer の Metropolis テーマで block の色を変える方法
- Misc___Taiwan/20250728_01.html
- 51 page views
- title: 「我那小小多山的國家」という言葉が流行っている
- Food___Taiwan/20230608_04.html
- 50 page views
- title: 「田園美食屋」の咖哩煎雞腿
- Computer___TeX/20240414_01.html
- 50 page views
- title: LuaTeX を使って日本語と中国語の漢字が混在した PDF ファイルを作成する方法
- Computer___Python/20240101_00.html
- 49 page views
- title: Matplotlib の 3D plot においての注意点
- Computer___FreeBSD/20220621_0.html
- 49 page views
- title: FreeBSD での X.org の設定の仕方
- Misc___Japan/20240610_00.html
- 47 page views
- title: NHK ラジオの「聞き逃し」の変更点 (2024 年 06 月)
- Computer___NetBSD/20250409_00.html
- 45 page views
- title: Raspberry Pi 5 に NetBSD-10.1 をインストールする手順
- Computer___NetBSD/20241102_00.html
- 44 page views
- title: ImageMagick の convert コマンドについて
- Science___Math/20220420_0.html
- 44 page views
- title: ラプラシアンの三次元極座標表示
- Computer___TeX/20240410_00.html
- 43 page views
- title: pdfTeX を使って PDF ファイルを作る方法
- Food___Taiwan/20230711_15.html
- 43 page views
- title: 「東方美早餐店」での朝ご飯 (2023 年 06 月中旬)
- Computer___TeX/20240411_00.html
- 41 page views
- title: LuaTeX を使って PDF ファイルを作成する方法
- Computer___NetBSD/20240810_00.html
- 40 page views
- title: Raspberry Pi 5 に NetBSD-10 をインストールしてみた
- Computer___NetBSD/20250728_00.html
- 37 page views
- title: pkgsrc の geography/gdal-lib について
- Computer___Network/20230508_00.html
- 36 page views
- title: git push するときにパスワードの入力を省略する方法
- Misc___Taiwan/20220831_4.html
- 36 page views
- title: 台南の民權派出所
HTML file generated by Kinoshita Daisuke.