Python Of try Statement is used to handle the execution of the Python Exception thrown by interpreter . When the interpreter throws an error , The execution of the program will suddenly stop . To avoid this situation we can use try Statement to capture and handle exceptions programmatically .
try:
#your code that may throw exceptions
statement(s)
except Exception1:
#If Exception1 is thrown, then execute this block.
statement(s)
except Exception2:
#If Exception2 is thrown, then execute this block.
statement(s)
else:
#If there is no exception then execute this block.
statement(s)
Include code that you might throw exceptions in try In the block , Then follow the except Sentence block . You can deal with try Various exceptions thrown by the code in the statement block . If your try The code in a statement block can throw two types of exceptions , We can use two except The statement block handles the two exceptions separately . You can provide subsequent execution blocks for each possible exception type .
else Statement blocks are optional . If you provide else Sentence block , It will only try Statement block is executed when no exception is thrown .
In this example , We do a division of two numbers . When the divisor is zero Python The interpreter will throw an exception , We use except The statement block captures it .
# Example for Python Try Catch
a = 3
b = 0
c = 0
try:
c = a / b
except ZeroDivisionError:
print("b is zero. Correct the value or your logic.")
print(c)
Execution and output :
If we don't use try What happens to the statement block ?
What this kind of mistake says is , According to the definition of this method , It doesn't take any parameters , But we pass it a parameter .
The following example shows how to reproduce this error :
# TypeError: method() takes 0 positional arguments but 1 was given
class Laptop:
def details():
print("Hello! I am a laptop")
laptop1 = Laptop()
laptop1.details()
Execution and output :
You may have questions , Calling laptop1 Object's details() The method did not pass any parameters to it , But how to throw out this kind of TypeError Well ?
By default , If the method is not static Python Method , Will implicitly put the object (self) Passed to it as a parameter . therefore , Before you call laptop1.details() When , What is actually called is for laptop1.details(laptop1). So in order to comply with this inner behavior , We need to define details() Method is passed a parameter to it :
# Provide an argument in the definition of details() method
class Laptop:
def details(self):
print("Hello! I am a laptop")
laptop1 = Laptop()
laptop1.details()
Execution and output :
Of course , There's another way , It is defined as a static method . You can use annotations @staticmethod tell Python The interpreter states that this is a static method :
# Tell Python Interpreter that this method is a static method
class Laptop:
@staticmethod
def details():
print("Hello! I am a laptop")
laptop1 = Laptop()
laptop1.details()
Execution and output :