为什么这个C#代码会导致进程崩溃?

问题描述:

static void Main(string[] args) 
     { 
      try 
      { 
       var intValue = "test"; 
       var test = Convert.ToInt32(intValue); 
      } 
      catch (FormatException) 
      { 
       Console.WriteLine("format exception"); 
       throw; 
      } 
      catch (Exception) 
      { 

      } 
      finally 
      { 
       Console.WriteLine("finally"); 
      } 
     } 

据我所知,在从字符串转换为int的过程中,引发了一个FormatException。现在在catch块中,我们重新抛出原始异常。为什么这不会被捕获到泛型异常catch块?如果我把try/catch放在throw上,那么应用程序不会崩溃。为什么这个C#代码会导致进程崩溃?

为什么这不会被捕获到泛型异常catch块?

由于一般异常块捕获被抛出仅在try块,不赶上从catch块抛出的异常的异常。

所以,如果你打算从catch块中抛出一个异常,并且你想要处理它,你将需要在另一个try/catch中包装调用代码。