简体   繁体   English

处理异常

[英]Handling exceptions

I have a block of code inside a try catch block (c#). 我在try catch块(c#)中有一个代码块。 The block of code can throw two exceptions (ArgumentException/NullRefernceException). 代码块可以引发两个异常(ArgumentException / NullRefernceException)。

try
{
    //Code
}
catch(NullRefernceException Ex)
{
   //Error Handling Code
}
catch(ArgumentException Ex)
{
  //Error Handling code
}

The error handling code is the same in both the Exceptions. 两个异常中的错误处理代码相同。 So can i keep the error handling code in ArgumentException catch block and upon NullRefernceException can i throw ArgumentException since i have a catch block follwing it. 所以我可以将错误处理代码保留在ArgumentException catch块中,并且在NullRefernceException上可以抛出ArgumentException,因为我有一个跟随它的catch块。 Not sure whether it will work and does it have any harm on the performance and whether it is a good programming practice. 不确定它是否会工作,是否会对性能造成损害,以及它是否是好的编程习惯。

Am i left with no option but either to have the same code in both the catch blocks or to have a separate method holding the error handling code? 我是否别无选择,只能在两个catch块中使用相同的代码,或者使用单独的方法保存错误处理代码?

I don't want to keep the error handling code in a separate method and invoke. 我不想将错误处理代码保留在单独的方法中并调用。

  1. If you throw an ArgumentException within the catch of NullReferenceException, it will not be caught by the ArgumentException block here at all. 如果在NullReferenceException的捕获范围内抛出ArgumentException,则此处ArgumentException块将根本不会捕获该ArgumentException。 It will be thrown up to a HIGHER catch. 它将被扔到更高的捕获量。

  2. Throwing and catching of Exceptions is expensive. 抛出和捕获异常非常昂贵。 You really should not be doing it just to avoid writing the same code twice. 您确实不应该这样做,只是避免编写两次相同的代码。 To not repeat your code, just use a common handling method / class 要不重复代码,只需使用通用的处理方法/类

I always try to follow the DRY principle which stands for Don't Repeat Yourself, ie don't put redundant code because when you need to update something there's a potential chance that you might mess up something. 我总是尝试遵循DRY原则,该原则代表“不要重复自己”,即不要放置冗余代码,因为当您需要更新某些内容时,有可能会弄乱某些内容。 So, I'd recommend putting the common logic in a separate method and call it from both exceptions. 因此,我建议将通用逻辑放在单独的方法中,并从两个异常中调用它。

to resolve your problem you can create one method instead of writ same code in both catch block 要解决您的问题,您可以创建一个方法,而不是在两个catch块中编写相同的代码

for example 例如

try
{
    //Code
}
catch(NullRefernceException Ex)
{
   HandleError();
}
catch(ArgumentException Ex)
{
  HandleError();
}

Maybe you can 也许你可以

try
{

}
catch(Exception ex)
{
   if (ex is NullRefernceException || ex is ArgumentException)
   {
     //do something
   } 
   else 
   {
      //maybe re-throw the exception
   }
}
try
{
    //Code
}
catch(Exception Ex)
{
   //Error Handling Code for both cases
}

Keep it general since your try code only produce two types of exceptions and the error handling always the same 保持通用性,因为您的try代码仅产生两种类型的异常,并且错误处理始终相同

ArgumentException and NullReferenceException both inherit from SystemException so how about using ArgumentExceptionNullReferenceException都继承自SystemException所以如何使用

try
{
   //code
}
catch(SystemException EX)
{
   //handle error
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM