Thread: How to write all exception to log file?

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    How to write all exception to log file?

    In another lang. there is try catch so I can write the exception to log file , and fix that later.

    How can I do it in c? so if I have an error I can see it , write it to log , and my process will not droped

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,935
    C does not have a native exception feature. As such, you have to take care to check all relevant error status return values, error status output parameters, and/or call functions that tell you the error status of whatever you're working with, and then log the errors, if any, returning an error code in that function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    87
    Quote Originally Posted by C progammer View Post
    In another lang. there is try catch so I can write the exception to log file , and fix that later.

    How can I do it in c? so if I have an error I can see it , write it to log , and my process will not droped

    thanks
    Hi,

    You can redirect stderr to a file, or you can use IO means to open a file for writing , write in it, then close the file.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    On Windows under Visual Studio you can have structured exception handling for C.
    Be aware that your code won't work on other compilers.
    Structured Exception Handling (C/C++) | Microsoft Docs

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    625
    Quote Originally Posted by OldGuy2 View Post
    On Windows under Visual Studio you can have structured exception handling for C.
    Be aware that your code won't work on other compilers.
    Structured Exception Handling (C/C++) | Microsoft Docs
    I strongly recommend sticking to Standard C and stay away from "Windows extensions" and any other O/S and/or compiler extensions! Simple enough to write your own error handling function that can write out messages to a log file, that will work on any O/S, compiler combination.

    The OP has not stated the O/S or compiler being used.

  6. #6
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by rstanley View Post
    I strongly recommend sticking to Standard C and stay away from "Windows extensions" and any other O/S and/or compiler extensions! Simple enough to write your own error handling function that can write out messages to a log file, that will work on any O/S, compiler combination.

    The OP has not stated the O/S or compiler being used.
    That not so simple to handle each error that can happend, without any kind of "try catch"

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,391
    That not so simple to handle each error that can happend,
    True, but that's the C way.

  8. #8
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by OldGuy2 View Post
    On Windows under Visual Studio you can have structured exception handling for C.
    Be aware that your code won't work on other compilers.
    Structured Exception Handling (C/C++) | Microsoft Docs
    I use C on windows under VS
    But the code that on this link is for cpp

    can you please shoe me an example with try catch with c ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,935
    The C grammar is given in that article, and the main function in the example doesn't use anything C++ specific in itself. Anyway, as you can see SEH isn't a general exception feature such that you can easily throw your own exceptions; it provides a structured way to handle exceptions arising from "certain exceptional code situations, such as hardware faults, gracefully".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 11-15-2017, 06:39 PM
  2. Replies: 2
    Last Post: 08-30-2014, 11:07 PM
  3. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  4. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  5. Replies: 2
    Last Post: 03-04-2010, 04:19 AM

Tags for this Thread