0

Here, base may overflow beyond limits of int, causing runtime error, at which point I intend to catch the Runtime error raised, and handle it, so I tried the try-catch block, but it is not being caught.

int base=1;
try
{
     base *= 10;
     //some code
}
catch(...)
{
     //some code
}
Dwij Dixit
  • 23
  • 3

1 Answers1

5

You can't. Arithmetic overflow on signed integral types causes undefined behavior, not an exception. If such overflow happens in your program, it is too late. You can't do anything to save the program. It is already in undefined state at that point.

You should check for potential overflow before you perform the multiplication.

user17732522
  • 53,019
  • 2
  • 56
  • 105