try
is a statement, so it can appear anywhere a statement can appear. It is designed to allow you to handle exceptions.
try
{
//this code may throw!
}
After try
block, there must be one or more handling blocks (or simply: handlers), that are defined as catch
block:
catch(const std::exception& e)
{
}
catch(const MyException& e)
{
}
...
Formal parameter of catch
block determines, what types of exceptions can be caught (and causes this block to be entered).
When an exception of type
E
is thrown by any statement in compound-statement (try
block), it is matched against the types of the formal parametersT
of each catch-clause (catch
block) […], in the order in which the catch clauses are listed. The exception is a match if any of the following is true:
E
andT
are the same type (ignoring top-level cv-qualifiers onT
)T
is an lvalue-reference to (possibly cv-qualified)E
T
is an unambiguous public base class ofE
T
is a reference to an unambiguous public base class ofE
T
is a (possibly cv-qualified) pointer or a reference to a const pointer (since C++14), andE
is also a pointer, which is implicitly convertible toT
T
is a pointer or a pointer to member or a reference to a const pointer (since C++14), whileE
isstd::nullptr_t
.
Source: try-block.
solved what parameters are used in catch of try-catch block in c++? [closed]