LoginSignup
1
0

More than 3 years have passed since last update.

Pythonの当たり前

Posted at

構文エラーは、例外としてキャッチできるのか?

テストコード
try:
    while True print('Hello world')
except:
    print("シンタックスエラーをキャッチできた")

本来は、while True<ココ>に、コロン(:)が入る。
コロンないので、構文エラーになる。

出力結果
File "c:\HOS\RD\python入門\syntaxerror.py", line 2
  while True print('Hello world')

となり、例外はキャッチできない。
Pythonはインタプリタで1行ずつ処理するので例外がキャッチできても良いかと思ったが、構文解析に成功しない限りは、一行ずつの実行にならないので、例外処理ではキャッチできないのだろうと思った。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0