上的try-catch块的思考 [英] Thoughts on try-catch blocks

查看:123
本文介绍了上的try-catch块的思考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共无效DoSomething的()
{$ B:

这看起来像这样你对代码的想法$ b试
{
//实际的代码放在这里
}
赶上(异常前)
{
扔;
}
}



我看到的问题是实际的错误没有被处理,只是扔在不同的地方除外。我觉得更难以调试,因为我没有得到一个行号的实际问题。



所以我的问题是,为什么会这样好?



---- ----编辑



这是它看起来像大多数人的答案都在说这是毫无意义的做这没有自定义或特定的异常被抓。这就是我就想意见,正在陷入没有具体的异常时。我可以看到实际上做一些与捕获的异常,只是没有这个代码是这样的地步。


解决方案

根据什么你看它的质量是不是在不同的地方抛出异常。 抛没有目标重新抛出异常是由抛出异常有很大不同。主要是重新抛出不重置堆栈跟踪。



在这个特殊的样本,美中不足的是没有意义的,因为它没有做任何事情。唯一的例外是愉快地重新抛出,它是几乎一样,如果try / catch语句不存在。


What are your thoughts on code that looks like this:

public void doSomething()
{
    try
    {
       // actual code goes here
    }
    catch (Exception ex)
    {
        throw;
    }
}

The problem I see is the actual error is not handled, just throwing the exception in a different place. I find it more difficult to debug because i don't get a line number where the actual problem is.

So my question is why would this be good?

---- EDIT ----

From the answers it looks like most people are saying it's pointless to do this with no custom or specific exceptions being caught. That's what i wanted comments on, when no specific exception is being caught. I can see the point of actually doing something with a caught exception, just not the way this code is.

解决方案

Depending on what quality you are looking at it is not throwing the exception in a different place. "throw" without a target rethrows the exception which is very different from throwing an exception. Primarily a rethrow does not reset the stack trace.

In this particular sample, the catch is pointless because it doesn't do anything. The exception is happily rethrown and it's almost as if the try/catch didn't exist.

这篇关于上的try-catch块的思考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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