Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch run time error in C and C++?

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?

like image 376
R__ Avatar asked Jun 29 '11 14:06

R__


People also ask

How do you catch a run time error?

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.

Can we catch run time exception?

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.

What is run time error in C with example?

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.

Why do we get run time error in C?

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.


3 Answers

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. :)

like image 129
Armen Tsirunyan Avatar answered Oct 16 '22 20:10

Armen Tsirunyan


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.

like image 2
Dietrich Epp Avatar answered Oct 16 '22 22:10

Dietrich Epp


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.

like image 1
Blagovest Buyukliev Avatar answered Oct 16 '22 20:10

Blagovest Buyukliev