![]() |
Exception Handling is one of the most important topics from the C++ Interview perspective. Exception handling is an effective means to handle the runtime errors that disrupt the normal flow of the program. It is one of the most used concepts in real-world embedded systems so it is common to encounter questions based on exception handling during interviews. ![]() In this article, we will learn about the top 15 most frequently and most asked interview questions on Exception Handling. Exception Handling Interview Questions and Answers1. What is an Exception?Conditions responsible for creating errors during the execution of a program are known as Exceptions. These errors can interrupt the execution of the program and if the program can’t handle these exceptions then OS handles them and the program is terminated abruptly. 2. Explain Synchronous Exceptions and Asynchronous Exceptions?Synchronous exceptions are the exceptions that occur at a particular instruction. They can only be originated from throw expressions and are caused due to errors like incorrect input or array out-of-index access in a program. 3. What is Exception Handling?Conditions responsible for creating errors during the execution of a program are known as Exceptions. Handling these exceptions by either removing these conditions or by using some other operations than normal operations is known as exception handling. Exception handling is an effective means to handle the runtime errors that disrupt the normal flow of the program. 4. Why do we need exception Handling?We use Exception Handling for the following reasons:
5. How to implement exception handling in C++?C++ supports exception handling. It is implemented by try{ } and catch( ){ } statements.
It follows certain rules:
Example: try{ 6. What is the use of terminate( ) in C++?The terminate( ) function is used to abort the program by default rather than throwing it for a catch. The terminate( ) function can be used for the exceptions which can’t be handled instead we just abort them. The terminate() functions calls the terminate_handler which by default calls the abort() function. For example: if(x==0){ In this program, if x==0, it will automatically end the file with a note saying “terminate called without an active exception”. 7. What is the use of unexpected( ) in C++?The unexpected() function is called when the exception thrown by a function is a type not listed in the exception specification for the function. The unexpected( ) will call the unexpected_handler that by default calls the terminate(). 8. What will happen if an exception is thrown but not caught anywhere?When an exception is thrown but not caught anywhere, the program will terminate abnormally. 9. Explain the concept of Rethrowing exceptions?A rethrowing exception is a term used when we throw the exception again from one catch block to another. The exception is thrown towards another outside catch block. Example:
Output First throw called Rethrowing throw called 10. What is the difference between exception handling in C++ and Java?The following table list the differences between exception handling in C++ and Java:
11. Write the output of the following code with the explanation.
Output Default Exception Explanation: An integer value 10 is thrown as an exception. It can’t be interpreted as char * means we need to go with default catch that is why the “Default Exception” is printed. 12. Write the output of the following code with the explanation.
Output Before Error Exception Caught After catch Explanation: When a throw statement is encountered, the program control skips all the remaining statements of that block and goes directly to the corresponding catch block. That is why “After Error” is not printed. 13. Write the output of the following code with the explanation.
Output: Caught Base Exception Explanation: If both base and derived classes are caught as exceptions, then the catch block of the derived class must appear before the base class because if we put the base class first then the derived class catch block will never be reached. For example, the following C++ code prints “Caught Base Exception“ 14. Write the output of the following code with the explanation.
Output: An error will be raised Explanation: The default catch block should be the last catch block or else other catch blocks will never be reached. 15. Write the output of the following code with the explanation.
Output Inner Catch Outer Catch Explanation: “Inner Catch” is called followed by “Outer Catch” because of rethrowing exceptions. Bonus Questions:1. What is an error in C++?
Answer: Violation of syntactic and semantic rules of a languages 2. What program does by default when detecting an exception?
Answer: Termination of the program 3. Which of the following is an exception in C++?
Answer: A Number divided by zero 4. Throwing an unhandled exception causes standard library function _______________ to be invoked.
Answer: terminate() 5. Catch handler can have multiple parameters.
Answer: False 6. What will be the output of this code? Choose the correct option
Answer: The program crashed at runtime 7. What will be the output of this code? Choose the correct option
Answer: This is an exception |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |