Unit II Java & J2EE regarding Java application development
C++ catch blocks and throw expressions
1. catch blocks (C++ only)
catch block syntax
>>-catch--(--exception_declaration--)--{--statements--}--------><
You can declare a handler to catch many types of exceptions. The allowable objects that a
function can catch are declared in the parentheses following the catch keyword (the
exception_declaration). You can catch objects of the fundamental types, base and derived class
objects, references, and pointers to all of these types. You can also catch const and volatile
types. The exception_declaration cannot be an incomplete type, or a reference or pointer to an
incomplete type other than one of the following:
void*
const void*
volatile void*
const volatile void*
You cannot define a type in an exception_declaration.
You can also use the catch(...) form of the handler to catch all thrown exceptions that have
not been caught by a previous catch block. The ellipsis in the catch argument indicates that any
exception thrown can be handled by this handler.
If an exception is caught by a catch(...) block, there is no direct way to access the object
thrown. Information about an exception caught by catch(...) is very limited.
You can declare an optional variable name if you want to access the thrown object in the catch
block.
A catch block can only catch accessible objects. The object caught must have an accessible copy
constructor.
throw expressions (C++ only)
You use a throw expression to indicate that your program has encountered an exception.
throw expression syntax
>>-throw--+-----------------------+----------------------------><
'-assignment_expression-'
2. The type of assignment_expression cannot be an incomplete type, or a pointer to an incomplete
type other than one of the following:
void*
const void*
volatile void*
const volatile void*
The assignment_expression is treated the same way as a function argument in a call or the
operand of a return statement.
If the assignment_expression is a class object, the copy constructor and destructor of that object
must be accessible. For example, you cannot throw a class object that has its copy constructor
declared as private.
Rethrowing an exception (C++ only)
If a catch block cannot handle the particular exception it has caught, you can rethrow the
exception. The rethrow expression (throw without assignment_expression) causes the
originally thrown object to be rethrown.
Because the exception has already been caught at the scope in which the rethrow expression
occurs, it is rethrown out to the next dynamically enclosing try block. Therefore, it cannot be
handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks
for the dynamically enclosing try block have an opportunity to catch the exception.
The following example demonstrates rethrowing an exception:
#include <iostream>
using namespace std;
struct E {
const char* message;
E() : message("Class E") { }
};
struct E1 : E {
const char* message;
E1() : message("Class E1") { }
};
struct E2 : E {
const char* message;
E2() : message("Class E2") { }
};
void f() {
try {
cout<< "In try block of f()" <<endl;
3. cout<< "Throwing exception of type E1" <<endl;
E1 myException;
throwmyException;
}
catch (E2& e) {
cout<< "In handler of f(), catch (E2& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
throw;
}
catch (E1& e) {
cout<< "In handler of f(), catch (E1& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
throw;
}
catch (E& e) {
cout<< "In handler of f(), catch (E& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
throw;
}
}
int main() {
try {
cout<< "In try block of main()" <<endl;
f();
}
catch (E2& e) {
cout<< "In handler of main(), catch (E2& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
}
catch (...) {
cout<< "In handler of main(), catch (...)" <<endl;
}
}
The following is the output of the above example:
In try block of main()
In try block of f()
Throwing exception of type E1
In handler of f(), catch (E1& e)
Exception: Class E1
In handler of main(), catch (...)
The try block in the main() function calls function f(). The try block in function f() throws an object
of type E1 named myException. The handler catch (E1 &e) catches myException. The
handler then rethrowsmyException with the statement throw to the next dynamically enclosing try
block: the try block in the main() function. The handler catch(...) catches myException.