0

I have a block of code inside a try catch block (c#). The block of code can throw two exceptions (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. 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?

I don't want to keep the error handling code in a separate method and invoke.

Novice
  • 2,447
  • 3
  • 27
  • 33

6 Answers6

4
  1. If you throw an ArgumentException within the catch of NullReferenceException, it will not be caught by the ArgumentException block here at all. 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

Jagmag
  • 10,283
  • 1
  • 34
  • 58
  • @Daniel Joseph: You need to be *much* more specific than that. What error are you receiving, exactly? – Cody Gray - on strike Nov 24 '10 at 07:34
  • if i throw ArgumentException from a NullReferenceException catch block i get "ArgumentException Unhandled by the user " error and the message is Value does not fall within the expected range.Why do i get this error ? – Novice Nov 24 '10 at 07:39
  • @Daniel Joseph: That indicates that you do not have a "Higher" catch. Hence the exception you have thrown is basically coming up as an unhandled exception in your application. What you need to catch it is a `catch (ArgumentException)` block in the method that is calling the method from where you are throwing this exception – Jagmag Nov 24 '10 at 07:48
  • fine ..so throwing an exception will look for a handling in the calling method.a higher catch.. Thank you for your reply.. – Novice Nov 24 '10 at 08:30
2

I always try to follow the DRY principle which stands for Don't Repeat Yourself, i.e. don't put redundant code because when you need to update something there's a potential chance that you might mess up something. So, I'd recommend putting the common logic in a separate method and call it from both exceptions.

Sidharth Panwar
  • 4,606
  • 6
  • 27
  • 36
1

to resolve your problem you can create one method instead of writ same code in both catch block

for example

try
{
    //Code
}
catch(NullRefernceException Ex)
{
   HandleError();
}
catch(ArgumentException Ex)
{
  HandleError();
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

Maybe you can

try
{

}
catch(Exception ex)
{
   if (ex is NullRefernceException || ex is ArgumentException)
   {
     //do something
   } 
   else 
   {
      //maybe re-throw the exception
   }
}
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
-1
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

Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
-1

ArgumentException and NullReferenceException both inherit from SystemException so how about using

try
{
   //code
}
catch(SystemException EX)
{
   //handle error
}
jb.
  • 9,921
  • 12
  • 54
  • 90