+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

C++ Exception Handling Interview Questions and Answers

Explain how we implement exception handling in C++

Exception handling in C++ is implemented by using the try{} and catch(){} statements.

When a try block throws an exception, the program leaves the try block and enters the catch statement of the catch block.

If they type of the object thrown matches the arg type in the catch block, catch block is executed for handling the code.

If they are not caught, abort() function is executed by default.

When no exception is deteted or thrown then the control goes to the statement below the catch block.

Describe exceptions in C++?

Exceptions: Exceptions are certain disastrous error conditions that occur during the execution of a program. They could be errors that cause the programs to fail or certain conditions that lead to errors. If these run time errors are not handled by the program, OS handles them and program terminates abruptly, which is not good. So C++ provides Exception Handling mechanism.

Explain terminate() and unexpected() function

terminate() is a library function which by default aborts the program
It is called whenever the exception handling mechanism cannot find a handler for a thrown exception.

unexpected() is called when a function with an exception specification throws an exception of a type that is not listed in the exception specification for the function
A function declaration without a specification like throw(char*) may throw any type of exception, and one with throw() is not allowed to throw exceptions at all.

By default unexpected() calls terminate().

What is Synchronous Exceptions?

Errors that occur due to incorrect input data or incorrect handling of array indices (“out of range index”), memory overflow are known as Synchronous Exceptions

What is Asynchronous Exceptions?

The errors that are caused by events that are beyond control of the program are Asynchronous Exceptions. E.g. Keyboard interrupts

Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.