What happens if both catch and finally blocks throw exception?

前端 未结 5 593
谎友^
谎友^ 2020-12-13 18:26

What happens if both catch and finally blocks throw exception?

相关标签:
5条回答
  • 2020-12-13 18:46

    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,

    0 讨论(0)
  • 2020-12-13 18:54

    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.

    0 讨论(0)
  • 2020-12-13 18:56

    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.

    0 讨论(0)
  • 2020-12-13 19:03

    The last exception thrown is thrown.

    0 讨论(0)
  • 2020-12-13 19:03

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

    0 讨论(0)
提交回复
热议问题