C#如何将详细信息附加到异常

发布于 2025-02-14 00:35:35 字数 953 浏览 2 评论 0原文

我正在尝试处理工作功能中的异常。我的目标是只抛出一个例外,所有必要的详细信息要归功于调用功能,然后决定是否需要登录或提醒用户等。扔。这是一个简单的示例(console.writeline(),而不是我的记录函数,为简单起见):

try
{
    string arg = "[Important-Details]";
    Worker(arg);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString()); //this is where I would log or pop-up message
}

这是工作函数:

public static void Worker(string details)
{
    try
    {
        int i = 1;
        int j = 0;
        int k = i / j; //line90
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Failed function with details:{details} - {ex.ToString()}"); //shows line90
        throw; //shows this line
    }
}

writeline()函数将显示LINE90(与I / J的线路)。这提供了一些有用的信息 - 我有抛出例外的确切行 - 但它并没有告诉我谁称为工人。提出此例外,我失去了我写的详细信息(“失败的功能,详细信息:{delect}”),加上将行号重置为“ throw” line。


尽管可以通过查看两个日志来获得所需的所有信息,但我宁愿只放弃一个例外,然后决定如何处理它。是否有一种令人震惊的方法可以做到这一点,或者什么都不能做?

I'm trying to handle exceptions being thrown in a worker function. My goal is to only throw one exception with all necessary details up to the calling function, who then decides if it needs to be logged or to alert the user, etc. But I would prefer to preserve the function and line number where the exception was thrown. Here is a simple example (Console.WriteLine() in place of my logging function, for simplicity):

try
{
    string arg = "[Important-Details]";
    Worker(arg);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString()); //this is where I would log or pop-up message
}

and here is the worker function:

public static void Worker(string details)
{
    try
    {
        int i = 1;
        int j = 0;
        int k = i / j; //line90
    }
    catch (Exception ex)
    {
        Console.WriteLine(
quot;Failed function with details:{details} - {ex.ToString()}"); //shows line90
        throw; //shows this line
    }
}

The WriteLine() inside the worker function will show line90 (line with i / j). This provides some useful information - I have the exact line where the exception was thrown - but it doesn't tell me who called the worker. Throwing up this exception, and I lose the details I wrote ("Failed function with details:{details}"), plus it resets the line number to the "throw" line.


Although it is possible to attain all the information needed by looking at both logs, I would rather only pass up one exception and then make the decision of what to do with it. Is there a snazzy way to do this, or nothing can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2025-02-21 00:35:35

解决此问题的标准方法是在worker函数的异常处理程序中使用Innerexception,其中包含原始异常:

public static void Worker(string details)
{
    try
    {
        int i = 1;
        int j = 0;
        int k = i / j; //line90
    }
    catch (Exception ex)
    {
        // Provide the original exception as inner exception to the new exception
        throw new ApplicationException($"Worker failed with details: {details}", ex); 
    }
}

这允许添加其他详细信息和保留原始异常,以便调用ex.toString()在调用功能中提供此输出的详细信息和原始行号:

system.ApplicationException:工人失败了详细信息:
[重要的详细信息] ---> System.DivideByZeroException:尝试
除以零。在program..worker(字符串详细信息)中
D:\ Windows \ temp \ hpojx1gl.0.cs:第11行----内部异常的结尾
堆栈跟踪---在program..worker(字符串详细信息)中
D:\ Windows \ temp \ hpojx1gl.0.cs:15行
D:\ Windows \ temp \ hpojx1gl.0.cs:第23行

我已经在

The standard way to solve this is to threw a new exception in the exception handler of the Worker function with an InnerException that contains the original exception:

public static void Worker(string details)
{
    try
    {
        int i = 1;
        int j = 0;
        int k = i / j; //line90
    }
    catch (Exception ex)
    {
        // Provide the original exception as inner exception to the new exception
        throw new ApplicationException(
quot;Worker failed with details: {details}", ex); 
    }
}

This allows to add additional details and preserves the original exception, so that the call to ex.ToString() in the calling function gives this output with both the details and the original line number:

System.ApplicationException: Worker failed with details:
[Important-Details] ---> System.DivideByZeroException: Attempted to
divide by zero. at Program.Worker(String details) in
d:\Windows\Temp\hpojx1gl.0.cs:line 11 --- End of inner exception
stack trace --- at Program.Worker(String details) in
d:\Windows\Temp\hpojx1gl.0.cs:line 15 at Program.Main() in
d:\Windows\Temp\hpojx1gl.0.cs:line 23

I've built a sample on dotnetfiddle; the division by zero is done in line 11 in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文