1

是否可以将参数传递给 catch 块?这是一些示例代码:

try 
{           
    myTextBox.Text = "Imagine, that could fail";
}
catch (Exception e)
{
    MessageBox.Show(e.Message); 
}

如果失败,我现在可以将文本框 ( myTextBox ) 传递给我的 catch 块吗?某事 像那样:

try 
{           
    myTextBox.Text = "Imagine, that could fail";
}
catch (Exception e, TextBox textBox)
{
    textBox.BorderBrush = Colors.Red;
    MessageBox.Show(e.Message); 
}

我会怎么做?

4

4 回答 4

6

不,标准是不可能的。

可以做的是定义您的自定义异常并在那里分配参数,例如:

public class MyCustomException : Exception
{
    public string SomeAdditionalText {get;set;}
    ....
    //any other properties
    ...
}

并在引发异常的方法中引发你自己的MyCustomException

于 2012-05-14T07:03:57.110 回答
6

You only catch a single thing, which in C# must be an Exception. So not directly. However! If the Exception were, say, a custom SomethingSpecificException, then you could make that information available on e.SomeProperty.

public class SomethingSpecificException : Exception {
    public Control SomeProperty {get;private set;}
    public SomethingSpecificException(string message, Control control)
          : base(message)
    {
       SomeProperty = control;
    }
    ...
}

Then at some point you could:

throw new SomethingSpecificException("things went ill", ctrl);

and

catch(SomethingSpecificException ex) {
    var ctrl = ex.SomeProperty;
    ....
}
于 2012-05-14T07:05:56.283 回答
1

不知道你想要实现什么,但是在 catch 块中你可以访问任何 UI 元素,就像在 try 块中一样。所以对我来说,在 catch 块中定义一个额外的参数是没有意义的。

于 2012-05-14T07:04:14.960 回答
0

Next to the possibility to use custom exception to distinguish what's going on:

try
{
    myClass.DoSomethingThatCouldThrow();
    myClass.DoSomethingThatThrowsSomethingElse();
    myClass.DoAnotherThingWithAThirdExceptionType();
}
catch(FirstSpecialException ex)
{
    // Do something if first fails...
}
catch(SecondSpecialException ex)
{
    // Do something if second fails...
}

You could also put every statement into its own exception block. This would make your code quite lengthly, but is maybe the only possibility if you can't change the class to throw any special exception.

try
{
    myClass.DoSomethingThatCouldThrow();
}
catch(InvalidOperationException ex)
{
    // Do something if it fails...
}

try
{
    myClass.DoSomethingThatCouldThrow();
}
catch(InvalidOperationException ex)
{
    // Do something if it fails...
}

try
{
    myClass.DoAnotherThingWithAThirdExceptionType();
}
catch(InvalidOperationException ex)
{
    // Do something if it fails...
}

Due to the fact, that this last, looks a little bit like repetitive code, we could maybe put it into some method with the following body:

public void TryCatch<ExceptionT>(Action tryMethod, Action<ExceptionT> catchMethod)
    where ExceptionT : Exception
{
    // ToDo: ArgumentChecking!

    try
    {
        tryMethod();
    }
    catch(ExceptionT ex)
    {
        catchMethod(ex);
    }
}

Which could you then call with:

TryCatch<InvalidOperationException>(
    () => myClass.DoSomething(),
    (ex) => Console.WriteLine(ex.Message));
于 2012-05-14T07:12:39.680 回答