开发者

What is the difference between trapping and handling an exception?

开发者 https://www.devze.com 2022-12-23 05:12 出处:网络
I\'m looking into exception handling in python and a blog post I read differentiated between trapping and handling an exception. Can someone explain the core difference between these two, both in pyth

I'm looking into exception handling in python and a blog post I read differentiated between trapping and handling an exception. Can someone explain the core difference between these two, both in python specifically and the overall conceptual difference开发者_JAVA百科? A google search for 'exception trapping handling' isn't super-useful.


I would say that "trapping" and "catching" an exception are the same thing: you have to trap/catch it to be able to handle it, but the act of trapping it is not the same as handling it.

Trapping-but-not-handling = supressing, in other words. Handling implies that you actually do something with the information at your disposal: log it, throw it to the next level, perform some action if the exception is not entirely unexpected etc.etc.

Or to put it another way, trapping an exception means that you have a code construct into which exception-al circumstances will flow, and where you can choose to handle the information that you find there.


In terms of a conceptual difference, I'd define Trapping as adding code to limit the impact of an error extending to other parts of the code or being displayed by the OS to the user.

Handling an error would be doing something appropriate in response to the error.

From a pseudo-code stance:

try
  // Something which may cause an error - this is trapped by wrapping in a try/catch
catch
  //  doing something appropriate in response to the error occurring - handle it
finally
0

精彩评论

暂无评论...
验证码 换一张
取 消