Python 3エンジニア認定基礎試験-例外処理(raise文)-

1. 
次のコードの実行結果を選んでください。

def validate_input(value):
if not isinstance(value, int):
raise TypeError("Value must be an integer")
return value * 2

try:
print(validate_input("text"))
except TypeError as e:
print("Error:", e)

2. 
次のコードの実行結果として正しいものを選んでください。

def check_divisor(y):
if y == 0:
raise ZeroDivisionError("Divisor cannot be zero")
return 10 / y

try:
print(check_divisor(0))
except ZeroDivisionError as e:
print("Error:", e)

3. 
次のコードの出力として正しいものを選んでください。

try:
raise Exception("Generic error")
except Exception as e:
print(type(e).__name__, ":", e)

4. 
次のコードの実行結果を選んでください。

try:
raise TypeError("Invalid type")
except Exception as e:
print("Caught Exception:", e)

5. 
次のコードの実行結果を選んでください。

try:
raise KeyError("Key not found")
except KeyError as e:
print("Caught KeyError:", e)
raise

6. 
次のコードの実行結果を選んでください。

try:
raise ValueError("Invalid data provided")
except KeyError as e:
print("Caught KeyError:", e)
except ValueError as e:
print("Caught ValueError:", e)

7. 
次のコードの実行結果を選んでください。

try:
raise RuntimeError("Runtime issue")
except RuntimeError as e:
print("Caught:", e)
raise

8. 
次のコードで発生する例外の種類として正しいものを選んでください。

raise KeyError("Key not found")

9. 
次のコードで正しい出力を選んでください。

try:
raise RuntimeError("Unexpected error occurred")
except RuntimeError as e:
print(e)

10. 
次のコードの実行結果を選んでください。

def check_value(x):
if x < 0:
raise ValueError("Negative value not allowed")
return x * 2

try:
print(check_value(-5))
except ValueError as e:
print("Error:", e)

11. 
次のコードの実行結果として正しいものを選んでください。

def check_positive(x):
if x < 0:
raise ValueError("Negative values are not allowed")
return x

try:
print(check_positive(-10))
except ValueError as e:
print(e)

12. 
raise文の役割として正しいものを選んでください。

13. 
次のコードを実行した場合の結果として正しいものを選んでください。

try:
raise ValueError("Invalid value")
except ValueError as e:
print("Caught exception:", e)

14. 
次のコードでraise文の動作として正しい説明を選んでください。

if not isinstance(x, int):
raise TypeError("x must be an integer")

15. 
次のコードの実行結果を選んでください。

def check_divisible(a, b):
if b == 0:
raise ZeroDivisionError("Cannot divide by zero")
return a / b

try:
print(check_divisible(10, 0))
except ZeroDivisionError as e:
print("Error:", e)

16. 
次のコードの実行結果を選んでください。

try:
raise NameError("Variable not defined")
except ValueError as e:
print("Caught ValueError:", e)
except NameError as e:
print("Caught NameError:", e)

17. 
次のコードの実行結果を選んでください。

try:
raise TypeError("Custom type error")
except KeyError as e:
print("Caught KeyError:", e)
except Exception as e:
print("Caught Exception:", e)

18. 
次のコードの実行結果を選んでください。

try:
raise IndexError("Index out of range")
except KeyError as e:
print("Caught KeyError:", e)
except Exception as e:
print("Caught Exception:", e)

19. 
次のコードの実行結果を選んでください。

try:
raise ValueError("Test error")
except Exception as e:
print("Caught an exception")
raise e

20. 
次のコードの実行結果を選んでください。

try:
raise ValueError("This is a value error")
except ValueError as e:
print("Caught error:", e)

コメントを残すにはログインしてください。