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?
mul()doesn'treturna value. It's easy to find, if think about what your code does.return, within thecatch- you can also have a singlereturn, outsite the try/catch block (usually what I do)return short.MinValueafter thetry...catchor make the return type ashort?and returnnull(to avoid the problem thatshort.MinValuecould be a valid multiplication result).OverflowExceptionhappens.