Catching a crash within a library code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Catching a crash within a library code (/thread-35605.html) |
Catching a crash within a library code - ebolisa - Nov-21-2021 Hi, How can I catch errors within a library that I cannot modify? The following code doesn't catch it. TIA import some_lib try: #do some with the library except OSError as e: print('*** ERROR ***', str(e)) #<--- the lib code crushes but this line is not reached RE: Catching a crush within a library code - bowlofred - Nov-21-2021 That should work if the OSError is the exception thrown. Can you show the actual code and a traceback where it doesn't work? RE: Catching a crush within a library code - BashBedlam - Nov-21-2021 If you do this: import numpy array = numpy.array ([one, two, three])It will throw this error: That last line and in particular is the information you need to use the try/except block. Try this:import numpy try : array = numpy.array ([one, two, three]) except NameError as e : print('*** ERROR ***', str(e)) RE: Catching a crush within a library code - ebolisa - Nov-21-2021 (Nov-21-2021, 10:48 PM)bowlofred Wrote: That should work if the OSError is the exception thrown. Can you show the actual code and a traceback where it doesn't work?code which is the culprit: try: blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=False, # disable SSL/TLS server='blynk.cloud', # set server address port=443, # set server port heartbeat=30, # set heartbeat to 30 secs log=False # use print function for debug logging ) except OSError as e: print('ERROR***', e) #machine.reset()error: There's an intentional error in the code which is cousing the lib to crush.I just want to know why my print line is not reached or how to fix it. Because, in a eventual real crush, the PLC/microcontroller needs to reset. Thanks. RE: Catching a crush within a library code - ebolisa - Nov-21-2021 (Nov-21-2021, 10:58 PM)BashBedlam Wrote: If you do this:Thanks but I cannot. I'm dealing with a microcontroller. RE: Catching a crash within a library code - bowlofred - Nov-21-2021 Looks correct to me. Two things to try: Make sure that the code logic and micropython aren't doing anything odd here. This should be caught as well: try: raise OSError('Test error') blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=False, # disable SSL/TLS server='blynk.cloud', # set server address port=443, # set server port heartbeat=30, # set heartbeat to 30 secs log=False # use print function for debug logging ) except OSError as e: print('ERROR***', e) #machine.reset()And: try: blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=False, # disable SSL/TLS server='blynk.cloud', # set server address port=443, # set server port heartbeat=30, # set heartbeat to 30 secs log=False # use print function for debug logging ) except: print('ERROR***') #machine.reset()I see no reason that the OSError shouldn't be caught, but this test should catch any possible exception. Maybe try them and see if they are both caught or not. RE: Catching a crash within a library code - ebolisa - Nov-21-2021 (Nov-21-2021, 11:47 PM)bowlofred Wrote: I see no reason that the OSError shouldn't be caught, but this test should catch any possible exception. Maybe try them and see if they are both caught or not.Interesting: both codes catch the error. Thanks! ERROR*** Test error ERROR*** RE: Catching a crash within a library code - bowlofred - Nov-22-2021 That the second one is caught seems odd. How about this? ... except Exception as e: print(repr(e)) print(type(e)) RE: Catching a crash within a library code - ghoul - Nov-22-2021 Now this thread makes me wonder whether it would be possible to catch an exception if the library code does not propagate it. RE: Catching a crash within a library code - bowlofred - Nov-22-2021 If the library were to catch the exception and not re-raise it, then there would be nothing for the library caller to catch. I think the library has 3 choices: * Not catch the error (or raise its own error). These should be catchable by the caller. * Catch the error and perform a normal return. There's nothing for the caller to catch. It has to interpret the return value to determine success or problem or other. * Kill the interpreter via os._exit(). Nasty and shouldn't be done. The caller will not execute at all (and can do no cleanup). |