0

I want to test if my decimal Add is smaller than MAXVALUE and bigger than MINVALUE, with a Try... Catch Method. And if the numbers are bigger than MAXVALUE or smaller than MINVALUE the code should throw an Exception.

But my Code isn't working.

    public static decimal Add(decimal number1, decimal number2)
    {
        decimal Add = number1 + number2;

        try
        {
            Add > RESULT_MAXVALUE;
            Add < RESULT_MINVALUE;
        }

        catch(Exception)
        {
            //Do Stuf
        }
    }

I don't want to use if... else!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • why do you want to throw an exception? – Rich Aug 13 '15 at 06:36
  • Because I wnat my Console to do something afterwards, and I don't want to use if... else. Also I want to look if the exception contains the value of my Maxvalue or MINVALUE, but thats not the point, I Just want to know how I write the Things in the try block. – Console Argonaut Aug 13 '15 at 06:41
  • if ( RESULT_MINVALUE > Add || Add > RESULT_MAXVALUE ) throw new Exception("error"); that should do it. – Stultuske Aug 13 '15 at 06:44
  • Thank you, that works :D – Console Argonaut Aug 13 '15 at 06:46
  • 7
    Using exceptions to control program flow [is an awful idea](http://stackoverflow.com/questions/161942/how-slow-are-net-exceptions). – Uwe Keim Aug 13 '15 at 06:47
  • 2
    I'm not sure you understand the definition of what an exception is. @UweKeim is correct if your new to programing I would recommend not starting this bad habit. – jradich1234 Aug 13 '15 at 07:00

1 Answers1

1

It depends on the language that you're using, but the convention is that the try block contains statements that can throw exceptions, and thrown exceptions are caught by the catch() blocks following the try. You need to explicitly throw an exception before it can be caught.

It looks like you're using C#. Consider reading https://msdn.microsoft.com/en-us/library/0yd65esw.aspx for more information about try-catch statements in C#.

Using exceptions in your case may not be necessary. Consider using an if statement, like this:

decimal result = a + b;
if ((result > MAX_VALUE) || (result < MIN_VALUE))
{
    // Do stuff.
}

But to answer your question more directly, here's how you would do it using exceptions:

    decimal result = a + b;
    try
    {
        if ((result > MAX_VALUE) || (result < MIN_VALUE))
        {
            throw new System.ArithmeticException(); // Or make an exception type.
        }
    }
    catch (System.ArithmeticException e)
    {
        // Do stuff.
    }

Or perhaps you would throw the exception in Add, but not catch it. Then it would be the caller's responsibility to handle the exception, or let the program crash. That would look like this:

// Adds two numbers.  Throws a System.ArithmeticException if the result
// is greater than MAX_VALUE or less than MIN_VALUE.
public static decimal Add(decimal a, decimal b)
{
    decimal result = a + b;
    if ((result > MAX_VALUE) || (result < MIN_VALUE))
    {
        throw new System.ArithmeticException(); // Or make an exception type.
    }
}

And callers would need to wrap calls to Add in a try {} catch if they expect some results to be bigger than MAX_VALUE or less than MIN_VALUE (or, callers could not catch the exception and the program would crash).