CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    [RESOLVED] why should I use exception handling in my code?

    I've read about exception handling, but I don't understand why they are better than the old way.

    is exception handling faster?

    why should I do this:
    Code:
    void func(int x, int y)
    {
         if (x != y) throw "they are equal";
         else cout << x - y;
    }
    rather than this:

    Code:
    bool func(int x, int y)
    {
         if (x != y) return false;
         cout << x - y;
         return true;
    }
    I would use the second variant, because I write less when calling the function.
    So, why is exception handling better?

    P.S. the function is not supposed to be useful, it's just for showing the difference.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: why should I use exception handling in my code?


  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: why should I use exception handling in my code?

    Exception handling is not faster, it's slower, it has to unwind the stack when they are thrown. But it's not significant, you'll never notice a difference unless you do something dumb. Exceptions are supposed to be error cases, which happen infrequently. If you encounter an error once in a million cycles (a high rate,) you'll maybe loose a couple of milliseconds in that time. Exception are more standard in the current paradigm, although a lot of people still use the error return codes (I do usually :P)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: why should I use exception handling in my code?

    Better control of where you want program control to go if an error is encountered. If you have a sequence of function calls you want to make, especially if some of them are nested and you want control to return to the same spot if any of them fail, you can wrap the whole thing up in a try/catch block without having to check and propagate the return codes of all the functions and nested functions you call.

    You can pass more detailed information about what went wrong back to the caller.

  5. #5
    Join Date
    Apr 2008
    Posts
    118

    Re: why should I use exception handling in my code?

    I've read about exception handling, but I don't understand why they are better than the old way.
    They are not intended to be a replacement for returning values from functions.

  6. #6
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: why should I use exception handling in my code?

    thanks for all the info.
    And Chris_F, thanks a lot for the link. it was very useful.

  7. #7
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: [RESOLVED] why should I use exception handling in my code?

    by the way, a few more questions:

    a. is there a point to do something like this:

    Code:
    try
    {
        char* b = new b;
    }
    
    catch (...)
    {
    //here you should not be able to do anything unless you can release some memory from other places... if you can.
    }
    b. should I use a try - catch block for something like this:
    Code:
    char str[20];
    ?

    c. what happens if I use the STL's string class, but it cannot allocate memory? I assume that the program would crash... and if so, I don't understand why a raw string must be allocated using exception handlers: the program would crash anyway. Am I mistaken somewhere?

  8. #8
    Join Date
    Aug 2008
    Posts
    902

    Re: [RESOLVED] why should I use exception handling in my code?

    Generally speaking if you fail to allocate memory, the application will crash and there isn't much you can do about it. I know some garbage collectors will do an emergency collection on a failure to allocate memory to try and prevent a crash. Not really applicable unless you are using some kind of GC library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured