Thread: Exception handling

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Exception handling

    Hi! I just started using exception handling (never had chance before). What you guys think it is best:

    Code:
    try{
    //code that throws Exception_1
    
    //code that do not throws anything
    
    //code that throws Exception_2
    }
    catch(Exception_1 e1){
    //handle exceptions
    }
    catch(Exception_2 e2){
    //handle exceptions
    }
    Or:

    Code:
    try{
    //code that throws Exception_1
    }
    catch(Exception_1){
    //handle exceptions
    }
    
    //code that do not throws anything
    
    try{
    //code that throws Exception_2
    }
    catch(Exception_2){
    //handle exceptions
    }
    //handle exceptions

    I think that, for large pieces of code, the second alternative is better. Do you agree? Also, I have a question, when you use catch(...) m how can you access the exception itself?Is there any global or method, or I must specify the parameter?

    Thanks, as always.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Entirely depends on how you want your errors to be handled...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What you guys think it is best
    It's up to you. I could use either depending on how my code is otherwise structured.

    >when you use catch(...) m how can you access the exception itself?
    How do you know what the exception is? It could be an exception object derived from the standard exception, or it could be an int. You just don't know, so how can you expect to "access" it safely?
    My best code is written with the delete key.

  4. #4
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Thank you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM