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.

1

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

4 Comments

@Daniel Joseph: You need to be much more specific than that. What error are you receiving, exactly?
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 ?
@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
fine ..so throwing an exception will look for a handling in the calling method.a higher catch.. Thank you for your reply..
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.

Comments

1

Maybe you can

try { } catch(Exception ex) { if (ex is NullRefernceException || ex is ArgumentException) { //do something } else { //maybe re-throw the exception } } 

Comments

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(); } 

Comments

-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

Comments

-1

ArgumentException and NullReferenceException both inherit from SystemException so how about using

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

1 Comment

Not a good practice. It'll swallow all the other exceptions also :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.