user-0 提问: 请教C语言怎么捕获任何异常，并且把异常信息打印在文本文件里？谢谢

回复:
user: user-0
content: 引用 6 楼 yangyunzhao 的回复:楼主说的可能是类似段错误、doublefree等问题吧。一般都是捕获信号，然后打印堆栈 我想实现的是类似与java的try excetion的功能，捕获任何异常

user: user-1
content: 引用 7 楼 u011718046 的回复:[quote=引用 6 楼 yangyunzhao 的回复:] 楼主说的可能是类似段错误、doublefree等问题吧。一般都是捕获信号，然后打印堆栈 我想实现的是类似与java的try excetion的功能，捕获任何异常[/quote] C的标准的确没有异常机制，但即便是C++也无法像Java那样捕捉所有的异常，C/C++和Java还是有很大区别的。 楼主需要先了解下C/C++的基础知识。

user: user-1
content: 引用 2 楼 Automation_dmu 的回复:自己限定所有抛出的异常都派生于exception， 定义what()方法输出异常信息。 catch(exception& e){        //输出e.what()到文件. } 楼主说的是C，你说的是C++

user: user-2
content: 引用 7 楼 u011718046 的回复:[quote=引用 6 楼 yangyunzhao 的回复:] 楼主说的可能是类似段错误、doublefree等问题吧。一般都是捕获信号，然后打印堆栈 我想实现的是类似与java的try excetion的功能，捕获任何异常[/quote]  setjmp/longjmp就是错误处理，一旦发现一个不可恢复的错误，可以把控制转移  try catch就是建立在 setjmp/longjmp的基础上的      #include <stdio.h>       #include <setjmp.h>              jmp_buf buf;              void test_longjmp(void)       {         printf("test_longjmp() \n");         longjmp(buf, 1);         printf("end test_longjmp()\n");       }              void main(void)       {          if(setjmp(buf))            printf("back test_longjmp");          else            {              printf("first time to setjmp \n");              test_longjmp();            }       }

user: user-3
content: 仅供参考longjmp Restores stack environment and execution locale.  void longjmp( jmp_buf env, int value );  Routine Required Header Compatibility  longjmp <setjmp.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  None  Parameters  env  Variable in which environment is stored  value  Value to be returned to setjmp call  Remarks  The longjmp function restores a stack environment and execution locale previously saved in env by setjmp. setjmp and longjmp provide a way to execute a nonlocal goto; they are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the normal call and return conventions.  A call to setjmp causes the current stack environment to be saved in env. A subsequent call to longjmp restores the saved environment and returns control to the point immediately following the corresponding setjmp call. Execution resumes as if value had just been returned by the setjmp call. The values of all variables (except register variables) that are accessible to the routine receiving control contain the values they had when longjmp was called. The values of register variables are unpredictable. The value returned by setjmp must be nonzero. If value is passed as 0, the value 1 is substituted in the actual return.  Call longjmp before the function that called setjmp returns; otherwise the results are unpredictable.  Observe the following restrictions when using longjmp:   Do not assume that the values of the register variables will remain the same. The values of register variables in the routine calling setjmp may not be restored to the proper values after longjmp is executed.    Do not use longjmp to transfer control out of an interrupt-handling routine unless the interrupt is caused by a floating-point exception. In this case, a program may return from an interrupt handler via longjmp if it first reinitializes the floating-point math package by calling _fpreset.   Be careful when using setjmp and longjmp in C++ programs. Because these functions do not support C++ object semantics, it is safer to use the C++ exception-handling mechanism.  Example  /* FPRESET.C: This program uses signal to set up a  * routine for handling floating-point errors.  */  #include <stdio.h> #include <signal.h> #include <setjmp.h> #include <stdlib.h> #include <float.h> #include <math.h> #include <string.h>  jmp_buf mark;              /* Address for long jump to jump to */ int     fperr;             /* Global error number */  void __cdecl fphandler( int sig, int num );   /* Prototypes */ void fpcheck( void );  void main( void ) {    double n1, n2, r;    int jmpret;    /* Unmask all floating-point exceptions. */     _control87( 0, _MCW_EM );    /* Set up floating-point error handler. The compiler     * will generate a warning because it expects     * signal-handling functions to take only one argument.     */     if( signal( SIGFPE, fphandler ) == SIG_ERR )     {       fprintf( stderr, "Couldn't set SIGFPE\n" );       abort();   }     /* Save stack environment for return in case of error. First      * time through, jmpret is 0, so true conditional is executed.      * If an error occurs, jmpret will be set to -1 and false      * conditional will be executed.     */    jmpret = setjmp( mark );    if( jmpret == 0 )    {       printf( "Test for invalid operation - " );       printf( "enter two numbers: " );       scanf( "%lf %lf", &n1, &n2 );       r = n1 / n2;       /* This won't be reached if error occurs. */       printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r );        r = n1 * n2;       /* This won't be reached if error occurs. */       printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r );    }    else       fpcheck(); } /* fphandler handles SIGFPE (floating-point error) interrupt. Note  * that this prototype accepts two arguments and that the   * prototype for signal in the run-time library expects a signal   * handler to have only one argument.  *  * The second argument in this signal handler allows processing of  * _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and   * _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols   * that augment the information provided by SIGFPE. The compiler   * will generate a warning, which is harmless and expected.   */ void fphandler( int sig, int num ) {    /* Set global for outside check since we don't want     * to do I/O in the handler.     */    fperr = num;    /* Initialize floating-point package. */    _fpreset();    /* Restore calling environment and jump back to setjmp. Return      * -1 so that setjmp will return false for conditional test.     */    longjmp( mark, -1 ); } void fpcheck( void ) {    char fpstr[30];    switch( fperr )    {    case _FPE_INVALID:        strcpy( fpstr, "Invalid number" );        break;    case _FPE_OVERFLOW:        strcpy( fpstr, "Overflow" );         break;    case _FPE_UNDERFLOW:        strcpy( fpstr, "Underflow" );        break;    case _FPE_ZERODIVIDE:        strcpy( fpstr, "Divide by zero" );        break;    default:        strcpy( fpstr, "Other floating point error" );        break;    }    printf( "Error %d: %s\n", fperr, fpstr ); }   Output  Test for invalid operation - enter two numbers: 5 0 Error 131: Divide by zero   Process and Environment Control Routines  See Also   setjmp

user: user-4
content: 自己限定所有抛出的异常都派生于exception， 定义what()方法输出异常信息。 catch(exception& e){        //输出e.what()到文件. }

user: user-2
content: C语言没有异常这一说，请自行进行合法性的判断。  然后，你可以  1.使用标准C库提供了abort()和exit()两个函数，强行终止程序的运行，<stdlib.h>。  2.使用assert(断言)宏调用，<assert.h>。  3.使用errno全局变量，<errno.h>。  4.使用goto跳转。  5.使用setjmp,longjmp跳转。

user: user-5
content: 楼主说的可能是类似段错误、doublefree等问题吧。一般都是捕获信号，然后打印堆栈

user: user-6
content: MSVC有SHE

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:仅供参考longjmp  谢谢，没看懂，