Python nâng cao từ khóa được sử dụng để tăng các ngoại lệ hoặc lỗi. Từ khóa RAFIC gây ra lỗi và dừng luồng điều khiển của chương trình. Nó được sử dụng để đưa ra ngoại lệ hiện tại trong một trình xử lý ngoại lệ để nó có thể được xử lý thêm trên ngăn xếp cuộc gọi. is used to raise exceptions or errors. The raise keyword raises an error and stops the control flow of the program. It is used to bring up the current exception in an exception handler so that it can be handled further up the call stack.
Cú pháp của từ khóa nâng cao:
REAVE & NBSP; {name_of_ the_ Except
Cách cơ bản để gây ra lỗi là:
Nâng cao ngoại lệ [văn bản người dùng của người dùng ”]
Example:
Trong mã dưới đây, chúng tôi kiểm tra xem một số nguyên chẵn hoặc lẻ. Nếu số nguyên là lẻ, một ngoại lệ được nâng lên. & nbsp;
Input:
Python3
def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
0def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
1 def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
2def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
3 def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
0def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
5 def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
6 def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
7def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
1 def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
9def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
03030def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
1def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
2 def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
3def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
4def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
5Output:
Trong khi nêu ra một lỗi, chúng ta cũng có thể loại lỗi nào chúng ta cần nêu ra và nếu cần in văn bản.
Syntax:
Nâng cao loại
Example:
Trong mã dưới đây, chúng tôi đã thử thay đổi chuỗi ‘Apple, & NBSP; được gán cho S thành Integer và viết một điều khoản Excet Try-Except để nâng cao giá trị. Từ khóa RAFIC làm tăng lỗi giá trị với thông báo Chuỗi Chuỗi có thể được thay đổi thành một số nguyên.
Input:
Python3
def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
6def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
1 def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
8def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
9def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
0def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
1raise ValueError['A very specific bad thing happened']
2def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
1 raise ValueError['A very specific bad thing happened']
4raise ValueError['A very specific bad thing happened']
5raise ValueError['A very specific bad thing happened']
6 raise ValueError['A very specific bad thing happened']
7def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
1def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
2 raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz']
0raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz']
1def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
5Đầu ra::
Nâng cao một ngoại lệ mà không chỉ định lớp ngoại lệ
Khi chúng tôi sử dụng từ khóa Raise, không có sự ép buộc nào để đưa ra một lớp ngoại lệ cùng với nó. Khi chúng tôi không đưa ra bất kỳ tên lớp ngoại lệ nào với từ khóa RAISE, nó sẽ xuất hiện ngoại lệ xảy ra lần cuối.
Example:
Trong mã trên, chúng tôi đã thử thay đổi chuỗi ‘Apple, thành Integer và đã viết một điều khoản Except thử để nâng cao giá trị. Mã này giống như trước đây ngoại trừ việc chúng tôi không cung cấp một lớp ngoại lệ, nó lại xuất hiện ngoại lệ xảy ra lần cuối.
Input:
Python3
def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
6def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
1 def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
8def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
9def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
0def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
1raise ValueError['A very specific bad thing happened']
2def demo_bad_catch[]:
try:
raise ValueError['Represents a hidden bug, do not catch this']
raise Exception['This is the exception you expect to handle']
except Exception as error:
print['Caught this error: ' + repr[error]]
>>> demo_bad_catch[]
Caught this error: ValueError['Represents a hidden bug, do not catch this',]
1 raise ValueError['A very specific bad thing happened']
4raise ValueError['A very specific bad thing happened']
5raise ValueError['A very specific bad thing happened']
6def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
0def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
1def demo_no_catch[]:
try:
raise Exception['general exceptions not caught by specific handling']
except ValueError as e:
print['we will not catch exception: Exception']
>>> demo_no_catch[]
Traceback [most recent call last]:
File "", line 1, in
File "", line 3, in demo_no_catch
Exception: general exceptions not caught by specific handling
2Output:
raise ValueError['A very specific bad thing happened']
6 raise ValueError['A very specific bad thing happened']
7
raise ValueError['A very specific bad thing happened']
raise ValueError['A very specific bad thing happened']
1def demo_no_catch[]: try: raise Exception['general exceptions not caught by specific handling'] except ValueError as e: print['we will not catch exception: Exception'] >>> demo_no_catch[] Traceback [most recent call last]: File "", line 1, in File "", line 3, in demo_no_catch Exception: general exceptions not caught by specific handling
2def demo_no_catch[]: try: raise Exception['general exceptions not caught by specific handling'] except ValueError as e: print['we will not catch exception: Exception'] >>> demo_no_catch[] Traceback [most recent call last]: File "", line 1, in File "", line 3, in demo_no_catch Exception: general exceptions not caught by specific handling
0raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz']
1raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz']
5def demo_no_catch[]: try: raise Exception['general exceptions not caught by specific handling'] except ValueError as e: print['we will not catch exception: Exception'] >>> demo_no_catch[] Traceback [most recent call last]: File "", line 1, in File "", line 3, in demo_no_catch Exception: general exceptions not caught by specific handling
- Đầu ra:
- Nâng cao một ngoại lệ mà không chỉ định lớp ngoại lệ
- Khi chúng tôi sử dụng từ khóa Raise, không có sự ép buộc nào để đưa ra một lớp ngoại lệ cùng với nó. Khi chúng tôi không đưa ra bất kỳ tên lớp ngoại lệ nào với từ khóa RAISE, nó sẽ xuất hiện ngoại lệ xảy ra lần cuối.
Từ khóa Python
Thí dụ
Tăng lỗi và dừng chương trình nếu X thấp hơn 0:
x = -1
Nếu x >> demo_no_catch[] Traceback [most recent call last]: File "", line 1, in File "", line 3, in demo_no_catch Exception: general exceptions not caught by specific handling 2 được sử dụng để nâng cao ngoại lệ.
Bạn có thể xác định loại lỗi nào để nêu và văn bản để in cho người dùng.
Nhiều ví dụ hơn
Thí dụ
Tăng lỗi và dừng chương trình nếu X thấp hơn 0:
x = -1
Nếu x