C++ introduces a new approach to error handling. Exception handling is based on keeping
the normal functionality of the program separate from error handling. The basic idea is
that errors occurring in one particular part of the program are reported to another part of
the program, known as the calling environment.

The calling environment performs central error handling. An application program no longer needs to continually check for errors, because in the case of an error, control is automatically transferred to the calling environment.

When reporting an error, specific information on the error cause can be added. This
information is evaluated by the error-handling routines in the calling environment.

Syntax of try and catch blocks

try
{
// Exceptions thrown by this block will be
// caught by the exception handlers,
// which are defined next.
}
catch( Type1 exc1)
{
// Type1 exceptions are handled here.
}
[ catch( Type2 exc2)
{
// Type2 exceptions are handled here.
}
. . . //etc.
]
[ catch( … )
{
// All other exceptions are handled here.
}]

try and catch Blocks

A try block contains the program code in which errors can occur and exceptions can be
thrown. Normally, a try block will consist of a group of functions that can produce similar errors.

Each catch block defines an exception handler, where the exception declaration, which is
enclosed in parentheses, defines the type of exceptions the handler can catch.

The catch blocks immediately follow the try block. A minimum of one catch block is
required. The exception handlers defined by the catch blocks catch the exceptions thrown
within the try block.

If there is no handler defined for a particular exception type, the program will not simply enter an undefined state but will be orderly terminated by a call to the standard function terminate().

It is common practice to define specific handlers for certain types of errors and one
generic handler for all other errors.

This functionality is provided by a special syntax in the catch statement with an exception declaration consisting of just three dots.

the application program decides what reaction is applicable for certain error conditions, the try and catch blocks are formulated in the application.

How Exception Handling Works

An exception handler catches the exception object thrown to it and performs error handling. The exception object type determines which handler will catch it and consequently be executed.

This means that you need to specify two things when implementing exception handling:

  • the part of the program that can throw exceptions
  • the exception handlers that will process the various exception types.

C++ provides language elements for this task, the keywords try and catch. Each keyword precedes a code block and thus they are often referred to as try and catch blocks.
Syntactically speaking, each try and catch block is a statement, however.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: what is Exception Handling In Cpp

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password