Thread: try

  1. #1
    Unregistered
    Guest

    try

    What does try { } do?

  2. #2
    brian0918
    Guest
    This is from the VC++ help files:





    try, catch, and throw Statements
    C++ Specific —>

    C++ exception handling uses the try, catch, and throw statements to implement exception handling. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. These exceptions are handled by code that is outside the normal flow of control.

    Note The Win32 structured exception-handling mechanism works with both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, throw) described in this topic.

    try-block :
    try compound-statement handler-list

    handler-list :
    handler handler-listopt

    handler :
    catch ( exception-declaration ) compound-statement

    exception-declaration :
    type-specifier-list declarator
    type-specifier-list abstract-declarator
    type-specifier-list
    ...

    throw-expression :
    throw assignment-expressionopt

    The compound-statement after the try clause is the guarded section of code. The throw-expression “throws” (raises) an exception. The compound-statement after the catch clause is the exception handler, and “catches” (handles) the exception thrown by the throw-expression. The exception-declaration statement indicates the type of exception the clause handles. The type can be any valid data type, including a C++ class.

    If the exception-declaration statement is an ellipsis (...), the catch clause handles any type of exception, including a C exception. Such a handler must be the last handler for its try-block.

    The operand of throw is syntactically similar to the operand of a return statement.

    Note Microsoft C++ does not support exception-specifications, as described in section 15.4 of the ANSI C++ draft. In addition, it does not support function-try-block described in section 15 of the ANSI C++ draft.

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Try getting a tutorial before u [bold]try[/bold]
    what does signature stand for?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Hey, come on Ruski, I really don't mind when someone asks a question like this because you don't hear it as often as other n00b questions

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    #define MY_EXCEPTION 1
    #define MY_EXCEPTION_2 2
    
    bool right;
    bool wrong;
    
    void function()
    {
         //stuff here
    
         if(!right)
              throw(MY_EXCEPTION);
    
         else if(wrong)
              throw(MY_EXCEPTION_2);
    }
    
    void fixTheProblem1()
    {
         right = true;
    }
    
    void fixTheProblem2()
    {
         wrong = false;
    }
    
    void callingFunction()
    {
         try
         {
              function();
         }
         catch (MY_EXCEPTION)
         {
              fixTheProblem1();
         }
         catch(MY_EXCEPTION_2)
         {
              fixTheProblem2();
         }
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Originally posted by Hunter2
    Code:
         if(!right)
              throw(MY_EXCEPTION);
    
         else if(wrong)
              throw(MY_EXCEPTION_2);
    Uhm......

  7. #7
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Originally posted by d00b
    Hey, come on Ruski, I really don't mind when someone asks a question like this because you don't hear it as often as other n00b questions
    What exactly do you mean?? And I wonder why the bold didnt work
    what does signature stand for?

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Maybe the bold woulda worked if you used [ b ] instead of [bold] After all, there is a "B" button, which stands for "Bold", and when you click it, it shows the proper bolding technique...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Hehe, you're right
    what does signature stand for?

  10. #10
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    The best introduction to using try is to use the dynamic memory allocation. If new fails it throws a bad_alloc exception. This means you cant see the code where throw is used but you can use try and catch to handle the failure.

    eg

    Code:
    int* ptr1;
    int* ptr2;
    
    try
    {
       ptr1 = new int[ 100 ];   //should work fine, only creating an array of 100 ints
    }catch( bad_alloc ex )
    {
       cerr << ex.what( );    
    }
    
    
    try
    {
      ptr2 = new int[ 10000000000000000 ];  //almost certainly gonna fail
    }catch( bad_alloc ex )
    {
       cerr << ex.what( ); 
    }
    Hope that helps...
    Couldn't think of anything interesting, cool or funny - sorry.

  11. #11
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Originally posted by endo
    The best introduction to using try is to use the dynamic memory allocation. If new fails it throws a bad_alloc exception. This means you cant see the code where throw is used but you can use try and catch to handle the failure.

    eg

    Code:
    int* ptr1;
    int* ptr2;
    
    try
    {
       ptr1 = new int[ 100 ];   //should work fine, only creating an array of 100 ints
    }catch( bad_alloc ex )
    {
       cerr << ex.what( );    
    }
    
    
    try
    {
      ptr2 = new int[ 10000000000000000 ];  //almost certainly gonna fail
    }catch( bad_alloc ex )
    {
       cerr << ex.what( ); 
    }
    Hope that helps...
    Nice avatar where did you get that?
    what does signature stand for?

  12. #12
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Cant remember now, I wanted an animated one like yours but settled for The Hulk after I saw the trailer for the new film
    Couldn't think of anything interesting, cool or funny - sorry.

  13. #13
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    You can get animated avatars at : yabb.info.com or something like that.. forgot
    what does signature stand for?

  14. #14
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    the try and catch keywords were implemented for the purpose of avoiding the ambiguities of returning error codes from functions.

    let's say you had a factorial function. it can't do negatives, so you decide that if it returned a negative, it was at error. that words for factorial functions, but what about natural logs? they can't input negative numbers, but that can output practically any real number. while you could assign one number of the 4 billion possible choices to signify an error, it breaks the code for that number, and it gives you one more constant to remember.

    the solution is try/catch. it removes any ambiguities over the error, and makes error handling code very clear.

Popular pages Recent additions subscribe to a feed