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
Eis thrown by any statement in compound-statement (tryblock), it is matched against the types of the formal parametersTof each catch-clause (catchblock) […], in the order in which the catch clauses are listed. The exception is a match if any of the following is true:
EandTare the same type (ignoring top-level cv-qualifiers onT)Tis an lvalue-reference to (possibly cv-qualified)ETis an unambiguous public base class ofETis a reference to an unambiguous public base class ofETis a (possibly cv-qualified) pointer or a reference to a const pointer (since C++14), andEis also a pointer, which is implicitly convertible toTTis a pointer or a pointer to member or a reference to a const pointer (since C++14), whileEisstd::nullptr_t.
Source: try-block.
solved what parameters are used in catch of try-catch block in c++? [closed]