Hướng dẫn how do you give a raise in python? - Làm thế nào để bạn tăng lương cho trăn?

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',]
0
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',]
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',]
2

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',]
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',]
0
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',]
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',]
7
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',]
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',]
9
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
03030

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
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
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
3
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
4
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
5

Output:

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
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',]
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
8

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
9
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
0

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
1
raise ValueError['A very specific bad thing happened']
2
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',]
1
raise ValueError['A very specific bad thing happened']
4
raise ValueError['A very specific bad thing happened']
5

raise ValueError['A very specific bad thing happened']
6
raise ValueError['A very specific bad thing happened']
7

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
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
2
raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz'] 
0
raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz'] 
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
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
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',]
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
8

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
9
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
0

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
1
raise ValueError['A very specific bad thing happened']
2
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',]
1
raise ValueError['A very specific bad thing happened']
4
raise ValueError['A very specific bad thing happened']
5

raise ValueError['A very specific bad thing happened']
6
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
0

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
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
2

Output:

raise ValueError['A very specific bad thing happened']
6
raise ValueError['A very specific bad thing happened']
7

  • 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
    
    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
    
    2
    raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz'] 
    
    0
    raise ValueError['A very specific bad thing happened', 'foo', 'bar', 'baz'] 
    
    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
    
    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.

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

Bài Viết Liên Quan

Chủ Đề