c# - 为什么此 C# 代码会使进程崩溃?

标签 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");
            }
        }

据我所知,在从字符串到整数的转换过程中,会抛出一个 FormatException。现在在 catch block 中,我们重新抛出原始异常。为什么这没有在通用异常捕获 block 中捕获?如果我将 try/catch 放在 throw 周围,那么应用程序就不会崩溃。

最佳答案

Why is this not caught in the generic exception catch block?

因为通用异常 block 捕获仅在 try block 中抛出的异常,而不会捕获从 catch block 中抛出的异常。

因此,如果您打算从 catch block 中抛出异常并且想要处理它,则需要将调用代码包装在另一个 try/catch 中。

关于c# - 为什么此 C# 代码会使进程崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42323957/

相关文章:

c# - 如何以及何时在 C# 中调用基类构造函数

c# - 如何创建不在 IIS 中托管的 ASP.NET Web 服务?

javascript - MVC Controller Action 返回 html 而不是 bool

c# - 帮助选择具有一对多关系的语句

c# - 仅在页面加载时连接到数据库一次

c# - 我能以某种方式表示 WPF 中 DataGrid 中的列表列表吗

c# - WPF 应用程序中 Controller 的位置是什么

c# - 如何使事件处理程序异步运行?

c# - 如何提高通过流下载大尺寸 azure blob 文件的性能?

c# - 如何更改实例化对象(克隆)的位置?