Fauch911 wrote: |
---|
"vector v[v.size()] my program just returns a 'random' value, so no exception is raised." |
The "
std::vector" has two means of accessing its elements: "
std::vector::at( )" and "
std::vector::operator []".
"
std::vector::at( )" will throw an exception if the index you give it is beyond the upper-bound index.
"
std::vector::operator []" will
not throw an exception if the index you give it is beyond the upper-bound index.
Fauch911 wrote: |
---|
"I dont understand what you mean by "cascading the exception to upper levels"." |
S/he means that when an exception is thrown inside a function, the exception will be thrown from one function to the next until the exception is caught. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
void Function_A( )
{
throw( an_exception );
}
void Function_B( )
{
Function_A( );
}
void Function_C( )
{
Function_B( );
}
int main( )
{
try
{
Function_C( );
}
catch( the_exception )
{
}
}
|
Here, "
Function_C( )" calls "
Function_B( )" which calls "
Function_A( )". "
Function_A( )" then throws an exception, but because "
Function_A( )" contains no "
catch" blocks, it cannot handle the exception. So, the exception is thrown to "
Function_B( )". Because "
Function_B( )" contains no "
catch" blocks, it cannot handle the exception so "
Function_B( )" re-throws the exception to "
Function_C( )". "
Function_C( )", too, contains no "
catch" blocks, so it too re-throws the exception to "
main( )". Because "
main( )" contains a "
catch" handler, the exception is handled and will not be thrown again unless explicitly told to do so.
Fauch911 wrote: |
---|
"In the program written here there is no "throw" command. So, how can an exception be caught in the first place?" |
Just because there's no visible "
throw" statement, one cannot assume that exceptions are never thrown by classes, functions and operators.
Wazzak