+ 2

Hi every one i would like to ask question what is difference between try except statement and raise statement?

THX

30th Jun 2024, 10:04 AM
Yusof
Yusof - avatar
6 Antworten
+ 7
The raise statement is not used for catching exceptions. It actually causes an exception, deliberately. Suppose you have a function that reads a sensor on an engine. If that sensor starts sending data that is outside the expected range, that is when you would want to raise an exception so that emergency measures can be taken. Somewhere else in the program in the hierarchy of calls to that function, a try/except would catch the exception and jump to the except: portion of the code and handle the emergency.
30th Jun 2024, 11:01 AM
Brian
Brian - avatar
+ 5
One of them raises an exception. The other detects when an exception occurs in a given suite of code. Can you guess which one does which functionality?
30th Jun 2024, 10:30 AM
Brian
Brian - avatar
+ 5
Yusof yes, your summary shows a good understanding of the statements.
30th Jun 2024, 11:50 AM
Brian
Brian - avatar
+ 2
@Brain Do you mean raise statement only catches error that may happen while try except handle the error? THX again
30th Jun 2024, 10:48 AM
Yusof
Yusof - avatar
+ 2
THX @Brain now I understand raise: It was made for making a deliberately exception if there is condition I have put as programmer is not satisfied that leads error message at the top of it traceback while try except : for unexpected error and handle it also. Am I right?
30th Jun 2024, 11:39 AM
Yusof
Yusof - avatar
+ 1
Thanks Brian for your kindness and your help.
30th Jun 2024, 9:19 PM
Yusof
Yusof - avatar