What happens if both catch and finally blocks throw exception?

余生颓废 提交于 2019-11-27 11:30:55

问题


What happens if both catch and finally blocks throw exception?


回答1:


When the finally block throws an exception, it will effectively hide the exception thrown from the catch block and will be the one ultimately thrown. It is therefore important to either log exceptions when caught, or make sure that the finally block does not itself throw an exception, otherwise you can get exceptions being thrown that are stifled and never seen.




回答2:


When catch throws an exception, finally block will be run and then exit with an exception. If the finally block throws an exception, the block will exit with an exception.




回答3:


The last exception thrown is thrown.




回答4:


Its already been answered well by adrianbanks, but the following post should be interesting: Interesting Exception Results: Throwing Exceptions From the Finally Block




回答5:


HI Nwaman i think you answer is wrong i have tested it in windows appliaction, i found if u write a program like the below one

try
{
    string s = "hu";
    int i = int.Parse(s);
}
catch (Exception ex)
{
    string s = "hu";
    int i = int.Parse(s);
    throw new Exception();
}
finally
{
    MessageBox.Show("hi");
}

and this will not result finally to excute,



来源:https://stackoverflow.com/questions/1482395/what-happens-if-both-catch-and-finally-blocks-throw-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!