krystian71115
krystian71115

Reputation: 1937

How to catch error when program crash?

How to catch error when program crashes?

Example - Java when crash writes an error log to file.

I writed this code to cause an error:

int * invalidPointer = NULL;
printf("%d\n", invalidPointer[0]);

And java crashes and saving an error log (hs_error_pid(pid).log) to file, I want to handle an error in program written with C (not in Java, this is only example)

Second example - Chrome when crashes shows information and we can restart browser by clicking yes.

Upvotes: 3

Views: 2005

Answers (1)

ForceBru
ForceBru

Reputation: 44838

Though it is undefined behavior, you can use this if you're getting segmentation fault.

#include <signal.h>
#include <stdio.h>

void Segfault_Handler(int signo)
{
    fprintf(stderr,"\n[!] Oops! Segmentation fault...\n");
}

int main() {
    signal(SIGSEGV,Segfault_Handler);
    return 0;
}

Upvotes: 6

Related Questions