C#为懒惰者提供方便的异常处理

C#为懒惰者提供方便的异常处理,c#,exception,C#,Exception,是否有任何可能的方法自动将所有字符串/等附加到我选择的文本框(或其他形式)中?现在,为了在主类之外的任何类中实现这一点,我必须将它路由到主类中,然后使用主类发布它 澄清(更改): 我不再只想处理异常。我真的只想从程序中的任何地方传递字符串/文本,而不必将其传递到主窗口 namespace MyProgram { public partial class MainWindow : Window { Main goes here... } } names

是否有任何可能的方法自动将所有字符串/等附加到我选择的文本框(或其他形式)中?现在,为了在主类之外的任何类中实现这一点,我必须将它路由到主类中,然后使用主类发布它

澄清(更改): 我不再只想处理异常。我真的只想从程序中的任何地方传递字符串/文本,而不必将其传递到主窗口

namespace MyProgram 
{
    public partial class MainWindow : Window
    {
        Main goes here...
    }

}

namespace People 
{
    public class Worker
    {
        public void printToLog()
        {
            textBoxErrorlog.AppendText("Message....");
        }
    } 
}

上面的代码不起作用。我必须将字符串返回到MainWindow类并从那里附加它(Worker中不存在bc textBoxErrorlog)。我想跳过这一步,从工人类发布它

您可以为创建自定义appender并让它执行此操作。除此之外,您还需要捕获异常并将其整理回UI线程。

您可以为其创建一个自定义appender,并让它执行此操作。除此之外,您还需要捕获异常并将其整理回UI线程。

您可以利用将事件从连接到,并将所有未处理的异常路由到您喜欢的任何位置

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += (sender, args) => { /* Write To Wherever */ };

我建议使用一个真正的处理程序等来充实它,但它给了您一个想法。

您可以利用从中连接事件,并将所有未处理的异常路由到您喜欢的任何位置

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += (sender, args) => { /* Write To Wherever */ };

我建议使用真正的处理程序等来充实这一点,但这会给你一个想法。

你的问题相当模糊。但我感觉你会发现这些活动很有趣/有用:

Application.ThreadException
AppDomain.CurrentDomain.UnhandledException
在添加代码后编辑:

一般来说,捕获
异常是不好的。在您的情况下,我建议挂接到
AppDomain.CurrentDomain.UnhandledException
,并在该处理程序中附加到主窗口的异常日志文本框

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        if (!e.IsTerminating)
        {
            MainWindow mw = GetRefToTheMainWindowSomehow();
            mw.AppendException(e.ExceptionObject);
        }
    }
在主窗口中:

    internal delegate void AppendExceptionDelegate(Exception e);

    public void AppendException(Exception e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new AppendExceptionDelegate(AppendException), new[] { e });
        }
        else
        {
            this._textBox.Text += e.Message;
        }
    }

你的问题很模糊。但我感觉你会发现这些活动很有趣/有用:

Application.ThreadException
AppDomain.CurrentDomain.UnhandledException
在添加代码后编辑:

一般来说,捕获
异常是不好的。在您的情况下,我建议挂接到
AppDomain.CurrentDomain.UnhandledException
,并在该处理程序中附加到主窗口的异常日志文本框

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        if (!e.IsTerminating)
        {
            MainWindow mw = GetRefToTheMainWindowSomehow();
            mw.AppendException(e.ExceptionObject);
        }
    }
在主窗口中:

    internal delegate void AppendExceptionDelegate(Exception e);

    public void AppendException(Exception e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new AppendExceptionDelegate(AppendException), new[] { e });
        }
        else
        {
            this._textBox.Text += e.Message;
        }
    }

“所有例外”是什么意思?你是说所有未处理的异常吗?最简单的方法是不捕获它们,然后你的应用程序将崩溃,你将不得不修复你的bug。添加代码!让我们看看你在做什么。说“将它导入”并不能描绘一幅清晰的画面。你说“所有例外”是什么意思?你是说所有未处理的异常吗?最简单的方法是不捕获它们,然后你的应用程序将崩溃,你将不得不修复你的bug。添加代码!让我们看看你在做什么。说“将它导入”并不能描绘一幅清晰的画面。我更新了我的问题。我在寻找一种更简单的做事方式。谢谢你迄今为止的帮助。我不知道你是否能比这简单得多。AppendException方法很复杂,因为GUI线程和控件在WinForms中的工作方式。WPF/XAML呢?我没有使用WinForms。没有办法只传递一个字符串而不是所有异常的东西?从上面的代码来看,我似乎只能在例外情况下使用它。我更改了示例以反映更多我想要的内容。我更新了我的问题。我在寻找一种更简单的做事方式。谢谢你迄今为止的帮助。我不知道你是否能比这简单得多。AppendException方法很复杂,因为GUI线程和控件在WinForms中的工作方式。WPF/XAML呢?我没有使用WinForms。没有办法只传递一个字符串而不是所有异常的东西?从上面的代码来看,我似乎只能在例外情况下使用它。我已经更改了我的示例,以反映更多我想要的。