“Meningkatkan pengecualian di Python” Kode Jawaban

Meningkatkan pengecualian di Python

>>> raise KeyboardInterrupt
Traceback (most recent call last):
...
KeyboardInterrupt

>>> raise MemoryError("This is an argument")
Traceback (most recent call last):
...
MemoryError: This is an argument

>>> try:
...     x = int(input("Enter a positive integer: "))
...     if x <= 0:
...         raise ValueError("That is not a positive number!")
... except ValueError as y:
...     print(y)
...    
Enter a positive integer: -2
That is not a positive number!
Outrageous Ostrich

Python menangkap semua pengecualian

try:
    raise Exception("Oh no! An error happened!")
except Exception as err:
    print("An error was handled")
finally:
  	print("This runs either way.")
Mattalui

Meningkatkan pengecualian di Python

>>> raise KeyboardInterrupt
Traceback (most recent call last):
...
KeyboardInterrupt

>>> raise MemoryError("This is an argument")
Traceback (most recent call last):
...
MemoryError: This is an argument

>>> try:
...     a = int(input("Enter a positive integer: "))
...     if a <= 0:
...         raise ValueError("That is not a positive number!")
... except ValueError as ve:
...     print(ve)
...    
Enter a positive integer: -2
That is not a positive number!
SAMER SAEID

Jawaban yang mirip dengan “Meningkatkan pengecualian di Python”

Pertanyaan yang mirip dengan “Meningkatkan pengecualian di Python”

Lebih banyak jawaban terkait untuk “Meningkatkan pengecualian di Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya