catch异常处理 [英] Catch Exception treatment

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

问题描述

有什么用之间的区别



 赶上(异常前)
{
$ ... b $ b罚球前;
}

和使用



  //捕捉可能包括(例外)
{
...
扔;
}


解决方案

扔前重新抛出从这一点异常对象。这通常是不好的,因为它破坏从而导致了原来的问题有用的调用堆栈信息。



罚球释放原始捕获的异常,从点是实际抛出。它保留的,而不是给你抓点到该点的调用堆栈信息。



赶上(例外)在本质上是一样的东西,除了明显的先为您提供了异常对象做的东西,第二个没有。但两者都将捕获所有异常。他们是来自赶上(SomeKindOfException)不同,这只会捕捉到特定类型的异常(或从该类型派生更具体的类型)。这是最好的情况一样:

 
{
//一些文件操作
}
赶上(FileNotFoundException异常fnfex)
{
//文件未找到 - 我们知道如何处理这种
}
//任何其他类型的异常,
//这是我们没有想到的,不能知道如何处理,
//不会被捕获并抛出通常


What's the difference between using

catch(Exception ex)
{
   ...
   throw ex;
}

and using

catch   //  might include  (Exception) 
{
   ...
   throw;
}

解决方案

throw ex re-throws the exception object from that point. This is generally bad, since it destroys the useful call stack information which lead up to the original problem.

throw releases the original caught exception, from the point it was actually thrown. It retains the call stack information up to that point, instead of to the point you caught.

catch(Exception) and catch are essentially the same thing, except obviously the first gives you the exception object to do something with, and the second does not. But both will catch all exceptions. They are distinct from catch(SomeKindOfException), which will only catch exceptions of that specific type (or more specific types derived from that type). This is best for situations like:

try
{
    //some file operation
}
catch(FileNotFoundException fnfex)
{
    //file not found - we know how to handle this
}
//any other kind of exception,
//which we did not expect and can't know how to handle,
//will not be caught and throw normally

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

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