How Can I Force Execution to the Catch Block?

后端 未结 12 1336
野趣味
野趣味 2021-01-17 07:46

I am wondering can try..catch force execution to go into the catch and run code in there?

here example code:

try {
    if (         


        
12条回答
  •  情书的邮戳
    2021-01-17 08:07

    As cadrel said, but pass through an Exception to provide more feedback, which will be shown in the innerException:

    try
    {
        if (AnyConditionTrue)
        {
            MethodWhenTrue();
        }
        else
        {
            HandleError(new Exception("AnyCondition is not true"));
        }
    }
    catch (Exception ex)
    {
        HandleError(ex);
    }
    

    ...

    private void HandleError(Exception ex) {
        throw new ApplicationException("Failure!", ex);
    }
    

提交回复
热议问题