0

I need to handle OverflowException in method mul().

class B
{
    short a, b;
    public B(short a, short b) { this.a = a; this.b = b; }
    public short mul()
    {
        try
        {
            return checked((short)(a * b));
        }
        catch (OverflowException exc) { Console.WriteLine(exc); }
    }
}

class MainClass
{
    public static void Main(string[] args)
    {
        B m1 = new B(1000, 500);
        m1.mul();
    }
}

But the above code gives the following error :Error CS0161: 'B.mul()': not all code paths return a value (CS0161)

What can I do to fix it?

5
  • Return a value after catching the exception Commented Jun 6, 2017 at 14:27
  • 1
    That means that there's a case where mul() doesn't return a value. It's easy to find, if think about what your code does. Commented Jun 6, 2017 at 14:29
  • All answers mention adding a 2nd return, within the catch - you can also have a single return, outsite the try/catch block (usually what I do) Commented Jun 6, 2017 at 14:30
  • return short.MinValue after the try...catch or make the return type a short? and return null(to avoid the problem that short.MinValue could be a valid multiplication result). Commented Jun 6, 2017 at 14:30
  • 1
    Your exception is handled, but your function does not return anything if the OverflowException happens. Commented Jun 6, 2017 at 14:39

4 Answers 4

5

Please, do not mix logic and UI; just put try {} catch {} to its proper place and everything will be clear:

class B 
{
    ...

    // Logic: multiply with possible Overflow exception
    // Let us be nice and document the exception 
    ///<exception cref="System.OverflowException">
    ///When a or (and) b are too large
    ///</exception> 
    public short mul()
    {
        // Do we know how to process the exception at the place? 
        // No. There're many reasonable responses: 
        // - stop execution
        // - use some special/default value (e.g. -1, short.MaxValue)   
        // - switch to class C which operates with int (or BigInteger) etc.
        // That's why we don't catch exception here  
        return checked((short)(a * b));
    }
}

...

class MainClass
{
    // UI: perform operation and show the result on the console
    public static void Main(string[] args)
    {
        B m1 = new B(1000, 500);

        try 
        { 
            m1.mul();
        }
        catch (OverflowException exc) 
        { 
            // Proper place to catch the exception: only here, at UI, 
            // we know what to do with the exception:
            // we should print out the exception on the Console
            Console.WriteLine(exc); 
        } 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

When exception is thrown you write something to console but don't return any value.

Your method return value is short so you should return some value in catch (because method should return some short value in every execution path or throw):

try 
{
    return checked((short)(a * b)); 
}

catch(OverflowException exc) 
{
    Console.WriteLine(exc);

    throw;
}

Comments

0

mul() does not return a value when an exception is caught. Add a return statement to the catch block or at the end of the method:

public short mul()
{
    try {
        return checked((short)(a * b)); }
    catch(OverflowException exc) {
        Console.WriteLine(exc);
        return 0; // or whatever
    }
    return 0; // this goes as well
}

Comments

0

You have to throw exception from catch block. For example:

catch(OverflowException exc) 
{
    Console.WriteLine(exc)
    throw exc;
} 

1 Comment

this will resolve OP compilation problem, but still is not a good advice. It introduce the need to check for a special value. Moreover negative numbers are possibly allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.