C# 要捕获的参数?

C# 要捕获的参数?,c#,try-catch,C#,Try Catch,是否可以将参数传递给catch块? 下面是一些示例代码: try { myTextBox.Text = "Imagine, that could fail"; } catch (Exception e) { MessageBox.Show(e.Message); } 如果文本框(myTextBox)失败,我现在可以将它传递给catch块吗?smth。就像这样: try { myTextBox.Text = "Imagin

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

try 
{           
    myTextBox.Text = "Imagine, that could fail";
}
catch (Exception e)
{
    MessageBox.Show(e.Message); 
}
如果文本框(myTextBox)失败,我现在可以将它传递给catch块吗?smth。就像这样:

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

我该怎么做呢?

不,这是不可能的

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

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

在引发异常的方法中,引发您自己的
MyCustomException

不,这在standart中是不可能的

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

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

在引发异常的方法中,引发您自己的
MyCustomException

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

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

您只需要
catch
一个单独的东西,在C#中它必须是
异常。所以不是直接的。然而!如果
异常
是一个自定义的
特定异常
,那么您可以在
e.SomeProperty
上提供该信息

public class SomethingSpecificException : Exception {
    public Control SomeProperty {get;private set;}
    public SomethingSpecificException(string message, Control control)
          : base(message)
    {
       SomeProperty = control;
    }
    ...
}
然后在某个时候你可以:

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


你只捕获一件事,在C语言中,它必须是一个例外。所以不是直接的。然而!如果
异常
是一个自定义的
特定异常
,那么您可以在
e.SomeProperty
上提供该信息

public class SomethingSpecificException : Exception {
    public Control SomeProperty {get;private set;}
    public SomethingSpecificException(string message, Control control)
          : base(message)
    {
       SomeProperty = control;
    }
    ...
}
然后在某个时候你可以:

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


除了可以使用自定义异常来区分发生了什么之外:

try
{
    myClass.DoSomethingThatCouldThrow();
    myClass.DoSomethingThatThrowsSomethingElse();
    myClass.DoAnotherThingWithAThirdExceptionType();
}
catch(FirstSpecialException ex)
{
    // Do something if first fails...
}
catch(SecondSpecialException ex)
{
    // Do something if second fails...
}
您还可以将每条语句放入它自己的异常块中。这将使您的代码非常长,但如果您无法更改类以引发任何特殊异常,这可能是唯一的可能性

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...
}
由于这最后一个看起来有点像重复代码,我们可以将其放入具有以下主体的某种方法中:

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

    try
    {
        tryMethod();
    }
    catch(ExceptionT ex)
    {
        catchMethod(ex);
    }
}
public void TryCatch(Action tryMethod,Action catchMethod)
where Exception:异常
{
//ToDo:正在检查!
尝试
{
tryMethod();
}
捕获(例外)
{
捕集法(ex);
}
}
您可以用以下哪种方式拨打电话:

TryCatch<InvalidOperationException>(
    () => myClass.DoSomething(),
    (ex) => Console.WriteLine(ex.Message));
TryCatch(
()=>myClass.DoSomething(),
(ex)=>Console.WriteLine(ex.Message));

在使用自定义异常来区分发生了什么的可能性旁边:

try
{
    myClass.DoSomethingThatCouldThrow();
    myClass.DoSomethingThatThrowsSomethingElse();
    myClass.DoAnotherThingWithAThirdExceptionType();
}
catch(FirstSpecialException ex)
{
    // Do something if first fails...
}
catch(SecondSpecialException ex)
{
    // Do something if second fails...
}
您还可以将每条语句放入它自己的异常块中。这将使您的代码非常长,但如果您无法更改类以引发任何特殊异常,这可能是唯一的可能性

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...
}
由于这最后一个看起来有点像重复代码,我们可以将其放入具有以下主体的某种方法中:

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

    try
    {
        tryMethod();
    }
    catch(ExceptionT ex)
    {
        catchMethod(ex);
    }
}
public void TryCatch(Action tryMethod,Action catchMethod)
where Exception:异常
{
//ToDo:正在检查!
尝试
{
tryMethod();
}
捕获(例外)
{
捕集法(ex);
}
}
您可以用以下哪种方式拨打电话:

TryCatch<InvalidOperationException>(
    () => myClass.DoSomething(),
    (ex) => Console.WriteLine(ex.Message));
TryCatch(
()=>myClass.DoSomething(),
(ex)=>Console.WriteLine(ex.Message));

关于这个问题,这不是有点过分了吗?=)@Mario大多数程序/上下文比一个10行左右的问题更复杂;有理由假设这个问题是真实事物的简化版本。所以:也许,也许不是。也许是。但是我想你可能有一个更简单的方法来解决这个问题。关于这个问题,这不是有点过分了吗?=)@Mario大多数程序/上下文比一个10行左右的问题更复杂;有理由假设这个问题是真实事物的简化版本。所以:也许,也许不是。也许是。但我在想,对于给定的上下文,你可能有一个更简单的解决方案。