抛出或尝试接球 [英] Throws or try-catch

查看:84
本文介绍了抛出或尝试接球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

决定是在方法中添加抛出子句还是使用 try-catch

What is the general rule of thumb when deciding whether to add a throws clause to a method or using a try-catch?

据我所知,当呼叫者中断时,应使用抛出当在方法内部执行的操作期间发生异常时,应使用合同的末尾(传递的对象)和 try-catch 。它是否正确?如果是这样,应该在呼叫方采取什么措施?

From what I've read myself, the throws should be used when the caller has broken their end of the contract (passed object) and the try-catch should be used when an exception takes place during an operation that is being carried out inside the method. Is this correct? If so, what should be done on the callers side?

PS:通过Google和SO搜索,但希望对此有一个明确的答案。

P.S: Searched through Google and SO but would like a clear answer on this one.

推荐答案


  • 仅当您可以以有意义的方式处理异常时,才捕获异常

  • 声明向上抛出异常(如果要由当前方法的使用者处理)

  • 如果异常是由输入参数引起的,则抛出异常(但这些异常通常未经检查)

  • 这篇关于抛出或尝试接球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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