1. Try {} catch {} in Python {}
2. Raise exception
3. try... try t... else ..
4. Finally Blocks
1. Try {} catch {} in Python {}
The keyword for exception handling in python is different from that in C #. The key for using try and retry t in python is as follows:
DefDive (x, y ):
Try :
Result = X / Y;
Except Zerodivisionerror as Z:
Print ( " Division by zero. " , Z );
Else :
Print ( " The result is " , Result );
Finally :
Print ( " Executing finally caluse. " );
2. Raise resume ption
In python, if you need to throw an exception in counter T, you can use the keyword raise, similar to the throw keyword in C.
Def Raise_test ():
Try :
Result = 1 / 0;
Except Zerodivisionerror:
Print ( " Can not process the exception, to throw... " );
Raise ;
Finally :
Print ( " In finally Block " );
Raise_test ();
3. try... try t... else ..
It is useful for code that must be executed if the try clause does not raise an exception
The else block is executed if the exception is not thrown.
4. Finally Blocks
A finally clauseis always executed before leaving thetrystatement
Finally blocks are executed, regardless of whether exceptions are generated.