What happens when you throw an exception in main()
When an exception is thrown, it is propagated up the function call stack. During this time, if the exception is caught, the program runs normally. What happens if the exception is still not caught in main(), that is, if it is thrown in main()? (The program crashes, but the results may vary slightly depending on the compiler)
main() begin…Test()terminate called after throwing an instance of ‘int’Aborted (core dumped)
main() begin…Test()The exception debugging dialog box is displayed
C++ supports custom terminating functions. If you set the custom terminating function by calling set_terminate(), the default terminal() function will be invalid;
Custom end functionNote:
1) You cannot throw an exception again in this function. This is the last chance to handle an exception.
Set_terminate () functionVoid (*)(); void(*)(); 2) The return value is the custom terminate() entry address;
2. What happens when an exception is thrown in a destructor
main() begin…Test()Void mterminate() // Call the custom termination function mterminate() for the first time in main()~Test()// exit(1) At the end of the program, the destructor is called. Abort () is called when the destructor throws an exception againAborted (core dumped) // Note: Some older versions of the compiler may call the custom terminate function mterminate(). Void mterminate()
main() begin…Test()void mterminate()~Test()// exit(1) At the end of the program, the destructor is called, in the destructor again throws an exception, the exception debugging dialog box will pop upNote: Some older versions of the compiler may call the custom termination function mterminate(), which displays void mterminate().
3. Abnormal specification of functions
To determine whether a function will throw an exception, we use the exception declaration directly. This is called the exception specification of the function.
The exception declaration is written after the argument list as a modifier to the function declaration;
[/url]
func1();
func2()
(
.
);
func3()
(a);
[/url]
func()terminate called after throwing an instance of ‘char’Aborted (core dumped)
func()Catch (char)// The exception was caught, indicating that the exception specification is not limited
In g++, when the exception is not in the function exception specification, a global function unexpected() is called, from which the default global terminating function terminate() is called;
However, in vs2013, exceptions are not limited by function exception specifications.
The g++ compiler follows the c++ specification, while the vs2013 compiler is not limited to this constraint.
; By calling set_unexpected()To set the custom exception function, at this time the system defaultGlobal function unexpected()Will the failure;
Abnormal functionFeatures: with default
Global function unexpected()As with the prototype, there is no parameter and no return value;
An exception can be thrown in a function (resuming program execution when the exception conforms to the exception specification of the triggering function; Otherwise, call global terminate() to terminate the program);
set_unexpected()
functionVoid (*)(); void(*)(); 2) Return value is custom
unexpected()Function entry address;
func()void m_unexpected()Catch (int)// The exception thrown by throw 1 in the custom exception function m_unexpected() is caught because it conforms to the function exception specification
func()Catch (char)// VS2013 does not follow the C ++ specification and is not restricted by the exception specification. It directly catches the throw ‘C’ in the function exception specification
Conclusion :(g++) unexpected() is the last chance to handle exceptions correctly. If not caught, terminate() is called and the current program ends up with an exception;
4. Analysis of dynamic memory application results
unexpected()The entry address of the function.
Func = 0 // => vs2013 and g++ does not define the global new_handler() function
func = 00401468Catch (const bad_alloc&)// Defines the global new_handler() function in the BCC and throws the STD ::bad_alloc exception in that function
operator new: 4Test()// NULL is returned because heap space failed, then create object on the failed heap space, when m_value = 0; When (equivalent to assigning a value to an invalid address), the compiler reported a segment errorSegmentation fault (core dumped)
operator new: 4pt = 00000000operator new[]: 24pt = 00000000
operator new: 4pt = 00000000operator new[]: 24pt = 00000000operator delete[]: 00000000
The new and delete operators, or new[] and delete[] operators, must be overloaded in order for different compilers to behave the same way when applying for memory. Instead of throwing STD ::bad_alloc, the STD ::bad_alloc exception is returned when a memory application fails. This must be done by throwing () to modify the memory requester function.
operator new: 4pt = 00000000operator new[]: 24pt = 00000000
5. New use of the new keyword
(1) nothrow keyword
0 // The nothrow keyword is used. If a dynamic memory application fails, NULL is returned
——————–
Catch (const bad_alloc&)// Throw STD ::bad_alloc exception on dynamic memory request failure without nothrow keyword
The memory request is too large, that is, the total size of the array must not exceed 0x7FFFFFFF bytes.
(2) Use new to create an object at the specified address
1:23:4
;
So in VS, when a dynamic memory request fails, STD :: BAD_alloc is thrown instead of returning NULL;