Exceptions in C use try and catch blocks as well as throw st

Exceptions in C++ use try and catch blocks, as well as throw statements. If a function needs to throw an exception, where is the best place for the following? try and catch block(s): throw statement(s):

Solution

If a function needs to throw an exception, then

The structure of try, throw and catch

                ReturnTYpe functioName(type arguments)

                                {

                                Statements;

                                try          {

                                                statements;

                                                throw exception;

                                                statements;

                                               }              // closing of try

                                catch(type exception)

                                                {

                                               Statement to handle exception;

                                                }

                                Remaining statement of function

                                } // closing of function

N.B: you can add multiple catch blocks as needed.

 Exceptions in C++ use try and catch blocks, as well as throw statements. If a function needs to throw an exception, where is the best place for the following?

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site