What is try and catch blocks in exception handling?

All QuestionsCategory: Cpp LanguageWhat is try and catch blocks in exception handling?
Chetan Shidling asked 4 years ago

I need short information.

2 Answers
chetan shidling answered 4 years ago

The following about try and catch blocks:

  1. If no exception is thrown in a try block, the catch block that is associated with the try block is ignored and control is transformed to the statements following the last catch block.
  2. If an exception occurs in a try block, the remaining statements in that block are ignored.
  3. The appropriate catch block is searched from the first catch and an appropriate catch block is selected.
  4. Once a catch block is executed, remaining catch blocks are ignored and control is transferred to the point immediately after the last catch block.
  5. The catch block that has three dots is designed to catch any type of exception.
chetan shidling answered 4 years ago

The following about try and catch blocks:

  • If no exception is thrown in a try block, the catch block that is associated with the try block is ignored and control is transformed to the statements following the last catch block.
  • If an exception occurs in a try block, the remaining statements in that block are ignored.
  • The appropriate catch block is searched from the first catch and an appropriate catch block is selected.
  • Once a catch block is executed, remaining catch blocks are ignored and control is transferred to the point immediately after the last catch block.
  • The catch block that has three dots is designed to catch any type of exception.

consider the following catch block:
catch(int a)

          //code to handle the exceptions
}
 
Observe the following block:

  • The parameter is called a catch block parameter.
  • The int type of the parameter indicates that it can catch an exception of type int.
  • A catch block can have zero parameters or one parameter. It should not have more than one parameter.
  • The value thrown by the try block will be equal to the parameter in the catch block.