[#17] – Exception Handling – Questions

C-plus-plus-programming-questions-techhyme

So far we have created programs in which we assumed that user would never input wrong data, code would never have bugs and programs always work properly without any error.

Index:

However, it is very rare to run the program successfully at very first attempt without any errors. Practically, every program may encounter various types of errors which produce an unexpected output or terminate the execution of the program abnormally or even may result in system crash.

The errors that occur at run time are known as exceptions.

Key Points To Remember –

  1. The errors that occur at run time are known as exceptions. The occur due to different conditions such as division by zero, accessing an element that is out of bound of an array, unable to open a file, running out of memory and many more.
  2. The exceptions are handled by creating a try-catch block. In the try block, we define the statement(s) that needs to be tested for exception(s) and if an exception occurs, we use the ‘throw’ statement to invoke an exception handler.
  3. In order to handle multiple exceptions of different types, we need to define multiple catch blocks associated with a single try block. Each of the multiple catch block defined take parameters of different data types and each must match a different type of exception.
  4. The catch(…) block handles an exception if it is not handled by any of the other catch block defined above.
  5. When a single catch block may not handle an exception completely then in such a situation, the catch block on receiving an exception must pass it to another catch block by rethrowing the exception.
  6. The exception specification helps you to throw a list of exceptions that a function can throw.

Similar Questions – 

  1. What are exceptions ?
  2. How is an exception handled in C++ ?
  3. What is the need of exception handling ?
  4. What is the purpose of keyword try, catch and throw in exception handling.
  5. What should be placed inside a try block and a catch block ?
  6. What happens when a raised exception is not caught by any of the catch block ?
  7. What is the need of multiple catch blocks ? How is it defined?
  8. How will you handle all the exceptions with a single catch block ?
  9. Why do we rethrow an exception ?
  10. What is an exception specification ? When is it used ?
You may also like:

Related Posts

Leave a Reply