PukiWiki


例外

_ 例外

_ C言語

なし

_ Perl

_ Python

>>> while True:
...     try:
...             x = int(raw_input("Please enter a number: "))
...             break
...     except ValueError:
...             print "Oops!  That was no valid number.  Try again..."
...
Please enter a number: fkpe
Oops!  That was no valid number.  Try again...
Please enter a number: foj
Oops!  That was no valid number.  Try again...
Please enter a number: afeafeaf
Oops!  That was no valid number.  Try again...
Please enter a number: 444444
>>> x
444444
>>>
import sys

try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except IOError as (errno, strerror):
    print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
    print "Could not convert data to an integer."
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise

例外を送出

>>> raise NameError, 'HiThere'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: HiThere

_ Ruby

 
Link: MenuBar(91d)
Last-modified: 2017-07-19 (水) 22:33:28 (91d)