Like modifying a CONST int
,
Can I register a specific function to handle run time errors so that this kind of operation just fails instead of terminating the application?
Runtime errors don't need to be explicitly caught and handled in code. However, it may be useful to catch them and continue program execution. To handle a runtime error, the code can be placed within a try-catch block and the error can be caught inside the catch block.
RuntimeException is intended to be used for programmer errors. As such it should never be caught. There are a few cases where it should be: you are calling code that comes from a 3rd party where you do not have control over when they throw exception.
Run-Time ErrorsErrors which are occurred after a successful compilation of program is said to be “run-time errors”. Number divisible by zero, array index out of bounds, string index out of bounds, etc. are most frequent run-time errors. These errors can't be very hard to detect at the compile time.
These errors indicate either a bug in your app's code, or a condition that the runtime library can't handle, such as low memory. End users of your app may see these errors unless your write your app to prevent them, or to capture the errors and present a friendly error message to your users instead.
If you mean C++, there is a certain exception class called runtime_error
. You can catch it with a catch clause:
catch(std::runtime_error& e) {}
However, many things in C and C++ (like modifying a const int) result in undefined behavior. You can't catch them at runtime. You can't catch them, because no exception is thrown (technically, anything may happen, including a throwing of exception (C++ only), but that's not something you can or should hope for).
The solution is to write clean safe code. For that there are many advices listed in many books. :)
Modifying a const
is, according to the specification, "undefined behavior" so the compiler can do anything. In practice, many implementations sometimes generate runtime errors for such code but many do not. It often depends on the nature of the program. Here is an illustration:
#include <stdio.h>
#include <string.h>
typedef int (*fn)(const char *);
extern const fn global_fn_ptr;
extern const char global_string[];
const fn global_fn_ptr = puts;
const char global_string[] = "hello";
int main(int argc, char *argv[])
{
puts("Setting global_fn_ptr to NULL");
*(fn *)&global_fn_ptr = NULL;
puts("Setting string to \"bye\"");
strcpy((char *)global_string, "bye");
return 0;
}
On my system, I get a SIGBUS from modifying the string, but modifying the function works fine. This is due to the peculiar nature of function pointers, whose value is not always determined at runtime so the value must be stored in writable memory.
It is generally not safe to catch a SIGBUS or SIGSEGV in C++ and turn it into an exception. It is also very difficult to correctly longjmp
out of a signal handler — half the code that uses this pattern in C is probably incorrect. The safest option is to let the program terminate immediately, or if you really need this kind of help from the runtime, work very carefully with C code so you can free the appropriate resources in a nonlocal exit — C++ won't do because longjmp
won't call destructors.
Or you can just move to C# or Java, both of which have runtimes that do this for you and garbage collectors that clean up afterwards.
This is operating system specific. The language itself specifies these as undefined behaviour.
In POSIX-compliant operating systems your program can catch a SIGSEGV
signal in case of a restricted memory access, SIGILL
in case of an invalid instruction or SIGFPE
in case of an illegal floating point operation, for example division by zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With