Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 哪个更有效??使用is对象并尝试捕获_C#_Asp.net_Try Catch - Fatal编程技术网

C# 哪个更有效??使用is对象并尝试捕获

C# 哪个更有效??使用is对象并尝试捕获,c#,asp.net,try-catch,C#,Asp.net,Try Catch,哪种代码更有效/更好。使用的是对象,如果是该类型的对象,则取消装箱??或者使用try-catch WizardStep parentWizardStep; try { parentWizardStep = (WizardStep)this.Parent; } catch { throw new Exception("Unable to cast parent control to this type."); } 或者这个: 如果在强制转换无效时需要抛出异常,为什么还要费

哪种代码更有效/更好。使用的是对象,如果是该类型的对象,则取消装箱??或者使用try-catch

WizardStep parentWizardStep;    
try
 {
   parentWizardStep = (WizardStep)this.Parent;
 }
catch
{
   throw new Exception("Unable to cast parent control to this type.");
}
或者这个:


如果在强制转换无效时需要抛出异常,为什么还要费心捕捉正常的
InvalidCastException
?无需额外代码即可强制转换:

WizardStep parentWizardStep = (WizardStep) Parent;
您当前正在将一个在其类型中表达其含义的异常(
InvalidCastException
-至少,您希望如此)替换为裸
异常
-为什么要丢失信息

如果相关值的类型不正确是一个bug,那么我通常会这样做。否则,使用
as
运算符和空检查:

WizardStep parentWizardStep = Parent as WizardStep;
if (parentWizardStep == null)
{
    // It wasn't a WizardStep. Handle appropriately
}
else
{
    // Use the WizardStep however you want
}

但是无论哪种方式,做一些你知道可能会抛出异常的事情都是很可怕的,在这种情况下很容易测试以避免异常,但是选择捕获它。效率会很低,但更重要的是,它只是不恰当地使用了异常。除此之外,您当前正在捕获所有异常。。。如果获取
this.Parent
会引发一些完全不同的异常,该怎么办?这可能与铸造无关。仅捕获特定的异常,除非您试图实现一些顶级的“捕获所有”处理程序(例如,中止服务器请求)。

使用as更有效:

WizardStep parentWizardStep = this.Parent as WizardStep;
if(parentWizardStep == null)
{
     // This isn't the right type
     throw new ApplicationException("Your exception here.");
}

// Use parentWizardStep here....

很好。我想这可能不仅仅是重新提出一个非法的例外,但如果不是,这是最好的选择…同意。如果您确实需要捕获异常并执行除重新抛出以外的操作,那么请使用try/catch块。否则就照@Jon说的去做。一个小小的诡辩。有时(虽然这可能不是其中之一),您确实希望捕获一个特定的异常,然后将其包装@Conrad:是的,有时您会将其包装,但正如您所说,您将捕获一个特定的异常,然后抛出一个不同的特定异常。。。传递不同的信息,同时在InnerException中保留原始信息。这里没有发生什么:)只是详细阐述Jon的评论:这将是一个使用异常控制业务逻辑的示例,这是一种不好的做法,因为它(a)给读者的印象是这种情况通常不会发生,(b)导致性能变差,(c)使高级调试会话更加困难(因为这里似乎有无数的例外,而不是真正的例外)。可能还有其他原因。
WizardStep parentWizardStep = this.Parent as WizardStep;
if(parentWizardStep == null)
{
     // This isn't the right type
     throw new ApplicationException("Your exception here.");
}

// Use parentWizardStep here....