对象在对象内部抛出异常 [英] objects throwing exceptions inside of objects

查看:73
本文介绍了对象在对象内部抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在某个对象上实例化另一个对象.该内部对象可能会引发异常.我可以按照KISS的原理,让该异常发生在处理父对象的任何代码上,这正是我想要做的.或者,我可以在父对象中进行一些异常处理,并且可能给客户端"代码一个更有意义的异常.是否要遵循一般规则,还是我要根据具体情况决定要做什么?

I'm working on an object that at some point instantiates another object. That inner object might throw an exception. I could just let that exception bubble up to whatever code is handling the parent object, which is what I want to do, following the philosophy of KISS. Or, I could do some exception handling within the parent object, and perhaps give a more meaningful exception to the 'client' code. Is there a general rule to follow, or do I decide what to do on a case-by-case basis?

使用子对象,我花了一些时间编写错误代码,因此在将来的某个时候,我可以向最终用户提供更有意义的错误消息.如果父对象和子对象都有自己的错误代码集,该如何处理?看来我会写一个异常处理程序来查看错误代码及其原始类,对吧?

With the child object, I took the time to write error codes, so at some point in the future I could give the end-user a more meaningful error message. If the parent object and the child object have their own set of error codes, how do I handle that? It seems I would write an exception handler that looks at the error code and its originating class, right?

推荐答案

如果外部"对象无法处理内部"对象引发的异常,则它一定不能尝试.充其量,它最多可以捕获一些更常见的信息,然后重新发送,并附带更多的信息.

If the "outer" object cannot handle the exceptions thrown by the "inner" object then it must not try to. At best it can catch a few of the more common ones and rethrow with slightly more information attached.

这篇关于对象在对象内部抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆