[Solved] what parameters are used in catch of try-catch block in c++? [closed]


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 parameters T 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 and T are the same type (ignoring top-level cv-qualifiers on T)
  • T is an lvalue-reference to (possibly cv-qualified) E
  • T is an unambiguous public base class of E
  • T is a reference to an unambiguous public base class of E
  • T is a (possibly cv-qualified) pointer or a reference to a const pointer (since C++14), and E is also a pointer, which is implicitly convertible to T
  • T is a pointer or a pointer to member or a reference to a const pointer (since C++14), while E is std::nullptr_t.

Source: try-block.

solved what parameters are used in catch of try-catch block in c++? [closed]