Which of the two exceptions was triggered?

If I have a procedure that can throw an ArgumentException in two places, something like ...

if (Var1 == null) { throw new ArgumentException ("Var1 is null, this cannot be!"); } if (Val2 == null) { throw new ArgumentException ("Var2 is null, this cannot be either!"); } 

What is the best way to determine in my calling procedure which of the two exceptions were thrown?

Or

Am I doing this wrong?

+7
c # exception-handling
source share
10 answers

Pass the variable name (Val1, Val2, etc.) in the second argument to the ArgumentException constructor. This will become a property of ArgumentException.ParamName.

+11
source share

For this particular scenario, you should use ArgumentNullException and correctly populate it with ParamName so that you know the argument, which is null.

The ArgumentException class also supports the same property, but you should use the most accessible type of exception.

When using these types of exceptions, also be careful when using a constructor that accepts both the message and the parameter name. Orders switch between each type of exception:

 throw new ArgumentException("message", "paramName"); throw new ArgumentNullException("paramName", "message"); 
+12
source share

Your caller should not care about which line threw the exception. In any case, a ArgumentException was thrown, and both should be treated the same.

+4
source share

Use the ArgumentException constructor (string, string) to determine which parameter was null.

 if (Var1 == null) { throw new ArgumentException ("Var1 is null, this cannot be!","Var1"); } if (Val2 == null){ throw new ArgumentException ("Var2 is null, this cannot be either!","Var2"); } 
+3
source share

The bigger question you should be asking yourself, why? If you are trying to disable some kind of logic, then exceptions are usually a bad way.

Rather, you should have a return type (or out / ref parameter) that will be set with a flag / value of some type that you can detect from the calling code to determine what the error is and turn off your logic.

If you insist on using exceptions, then in this case ArgumentNullException has a constructor that takes a parameter name and an exception message . You can throw an exception, and then when you catch the exception, open the ParamName property to determine the name of the parameter that caused the exception.

+3
source share

In fact, you do not provide enough information to answer your question. The obvious answer is to look at the exception message, but I assume this is not what you are looking for.

If it is really important that you can separate them programmatically, use another exception, or at least use the paramName property for the current exception constructor. This will give you some more relevant information.

However , using your own type of exception is the only way to ensure that you catch an exception for a particular circumstance . Since ArgumentException is part of the framework, it is possible that something else that you are throwing can throw it, which will lead you to the same catch block. If you create your own type of exception (one for both or one for each scenario), this will provide you with a way to handle a specific error. Of course, judging by your example, it seems that it would be easier to just check if Val1 or Val2 is null before you call the function to start.

+3
source share

Well, to throw an ArgumentException in particular, you have a parameter for which the argument had a problem:

 throw new ArgumentException("Var1 is null, this cannot be!", "Var1"); 

More generally, you usually do something like using different (possibly custom) exception types, and then the calling code may have different catch blocks

 public class MyCustomException1 : ApplicationException {} public class MyCustomException2 : ApplicationException {} try { DoSomething(); } catch(MyCustomException1 mce1) { } catch(MyCustomException2 mce2) { } catch(Exception ex) { } 
+2
source share
 try { //code here } catch (ArgumentException ae) { Console.WriteLine(ae.ToString()); } 

The output on the console will tell the message that you put.

0
source share

When throwing ArgumentExceptions you can always specify the name of the argument that throws the exception (it is a different constructor ). Of course, I assume that you really want to know which one was null, in which case you should probably use ArgumentNullException .

0
source share

If this is for a test case where you want to make sure that the correct exception message is displayed, I know that NUnit has an ExpectedMessage to an ExpectedException attribute. Otherwise, an ArgumentNullException is an ArgumentNullException , and your application should handle them anyway. If you want more details, create your own exception classes and use them.

So you can check the following:

 [ExpectedException(typeof(ArgumentNullException), ExpectedMessage="Var1 is null, this cannot be!"] public void TestCaseOne { ... } [ExpectedException(typeof(ArgumentNullException), ExpectedMessage="Var2 is null, this cannot be either!"] public void TestCaseTwo { ... } 
0
source share

All Articles