ahmed
ahmed

Reputation: 14666

In C# how to handle an invalid argument type exception before it occurs?

In C# how to handle an invalid argument type exception before it occurs

 static class calc
 {
    public static double subtract(double num1, double num2)
    {
        // I want to handle arguments exception here so whenever a nethod is called I can handle the exception

        return num1 - num2;
    }
  }

I want to handle exception if the user use wrong arguments' type.

Upvotes: 0

Views: 3340

Answers (5)

Jon Hanna
Jon Hanna

Reputation: 113242

You can't even compile this as accepting something that isn't double or can be converted to double. However, since compiling may happen close to execution (e.g. when the calling code is in an .aspx file), there is a bit of a point to this sometimes. It can also be convenient in such cases to have a form that accepts object parameters anyway so that the casting with the results of DataBinder.Eval doesn't have to be done on-page:

static class Calc
{
  public static double Subtract(double num1, double num2)
  {
     return num1 - num2; //always correct type
  }
  public static double Substract(object num1, object num2)
  {
     try
     {
        return Subtract((double)num1, (double)num2);
        //alternatively allowing looser matching:
        //return Subtract(Convert.ToDouble(num1) - Convert.ToDouble(num2));
     }
     catch
     {
        throw new Exception("My Custom Exception Text");
     }
  }
}

Frankly though, I'd find this more confusing than the default behaviour if I pass the wrong type from on-the-fly compiled code like that in .aspx as I'd know the common exceptions better than yours.

Upvotes: 0

Arie
Arie

Reputation: 5373

Do you mean something like, for example, this?

 static class calc
 {
    public static double subtract(double num1, double num2)
    {
        if (num1<num2) throw new ArgumentException("num1 < num2", "num2");
        return num1 - num2;
    }
  }

Upvotes: 0

Tipx
Tipx

Reputation: 7505

The only way I can see for this method to fail would be :

1) You call this method by reflection, but with bad parameters. This wouldn't be an argument exception since the problem would be "outside the call".

2) You call the function with such parameters that double - double != double. This wouldn't be exception if the user use wrong arguments' type though. For this, you can check your argument against double.Min :

static class calc
{
    public static double subtract(double num1, double num2)
    {
        double doubleAmountLeft = double.MaxValue - num1;
        if (num2 > num1)
        {
            throw new ArgumentOutOfRangeException("num2 > double.MaxValue - num1.");
        }

        return num1 - num2;
    }
}

Upvotes: 0

IAmGroot
IAmGroot

Reputation: 13855

verify that the parameters you are passing are actually valid types BEFORE you call the method.

Alternatively, Catch the exception using a try and catch around the call.

    try 
    {
         //Call Method
         subtract(1.0,2.0);
    }
    catch (Exception ex)
    {
       throw new FaultException(ex.Message);
       // Or display a message box.  
    }

Upvotes: 1

Digbyswift
Digbyswift

Reputation: 10400

In the case of your example, how could the arguments be invalid? Since you state the types, the values cannot be invalid.

The only scenario I can think of is if the (num1 - num2) < Double.MinValue in which case this would cause an error.

Upvotes: 4

Related Questions