What does try { } do?
What does try { } do?
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.
Try getting a tutorial before u [bold]try[/bold]
what does signature stand for?
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
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.
Uhm......Originally posted by Hunter2
Code:if(!right) throw(MY_EXCEPTION); else if(wrong) throw(MY_EXCEPTION_2);
What exactly do you mean?? And I wonder why the bold didnt workOriginally 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 does signature stand for?
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.
Hehe, you're right
what does signature stand for?
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
Hope that helps...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( ); }
Couldn't think of anything interesting, cool or funny - sorry.
Nice avatar where did you get that?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
Hope that helps...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( ); }
what does signature stand for?
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.
You can get animated avatars at : yabb.info.com or something like that.. forgot
what does signature stand for?
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.