C++第五章理论题整理2

When we found a exception in the program, We need to throw that and we handle that by using the catch keyword.

finally keyword will be executed at the end of all the exception.

User defined exceptions can be done by inheriting and overriding the exception class functionality.

There are two types of exception handling in c++. They are synchronous exception handling and asynchronous exception handling.

There are four runtime error messages associated with exceptions. They are overflow_error, range_error, system_error and underflow_error.

If there is many number of exceptions in the program means, We have to use multiple catch statement and it is hard to keep track of the program.

Exception are handled outside the regular code.

copy-constructor is mandatory for throwing a object.

As a constructor don’t have a return type, We have to throw the exception.

Resource is said to be leaked when it cannot be accessed by any means of standard mean.

If there is any mishap in memory or resource management means, the problems that are mentioned above can happen.

segmentation fault error can arise when there is a problem with memory.

As we are using a pointer value to copy a string, So it will be producing a runtime error.

The garbage collection attempts to reclaim memory occupied by objects that are no longer in use by the program.

catch(…) 

This catch statement will catch all types of exceptions that arises in the program.
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    class Polymorphic {virtual void Member(){}};
    int main () 
    {
        try
        {
            Polymorphic * pb = 0;
            typeid(*pb);   
        }
        catch (exception& e)
        {
            cerr << "exception caught: " << e.what() << endl;
        }
        return 0;
    }

In this program, We used a bad type id for the polymorphic operator, So it is arising an bad_typeid exception.

It will not throw an exception from the destructor but it will the process by using terminate() function.

C++ provides a mechanism to ensure that a given function is limited to throwing only a specified list of exceptions. It is called an exception specification.

Operations which are irreversible cannot throw anything.

compilers may try to move the catch-code far away from the try-code, which reduces the amount of code to keep in cache normally, thus enhancing performance.

It will be added to the overhead if an exception that is thrown may cause a whole load of objects to go out of scope.

The purpose of a constructor is to establish the class invariant. To do that, it often needs to acquire system resources or in general perform an operation that may fail.

There are two types of exceptions: Synchronous and asynchronous exceptions. Synchronous exceptions that are caused by the event which can be controlled by the program whereas Asynchronous exceptions are those which are beyond the control of the program.

 C++ asks the programmer to place the catch block of derived class before a catch block of the base class, otherwise derived catch block will never be executed.

Uncaught handler returns to its callee(i.e. the function it is called by).

If inner catch block is unable to handle the exception thrown then The compiler will check for appropriate catch handler of the outer try block

Return type of uncaught exceptions are bool.

When an error is raised means, it will be pushed into stack and it can be corrected later by the programmer.

As in the case of not using an exception, it will remain useless in the program and increase the code complexity.

here are nine standard exceptions in c++. They are bad_alloc, bad_cast, bad_exception, bad_function_call, bad_typeid, bad_weak_ptr, ios_base::failure, logic_error and runtime_error.

原文地址:https://www.cnblogs.com/hhlys/p/13495601.html