Skip to content

Latest commit

 

History

History
79 lines (57 loc) · 1.87 KB

File metadata and controls

79 lines (57 loc) · 1.87 KB

Exceptions

C++ 17 exception and no-exception

Text and Source code: https://msdn.microsoft.com/en-us/library/4tebey8h.aspx

“… When the catch statement is reached, all of the automatic variables that are in scope between the throw and catch statements are destroyed in a process that is known as stack unwinding …”

Text and Source code: https://docs.microsoft.com/en-us/cpp/cpp/exceptions-and-stack-unwinding-in-cpp?view=vs-2017

“… Prior to C++17 there were two kinds of exception specification. The noexcept specification was new in C++11. It specifies whether the set of potential exceptions that can escape the function is empty. The dynamic exception specification, or throw(optional_type_list) specification, was deprecated in C++11 and removed in C++17, except for throw(), which is an alias for noexcept(true). This exception specification was designed to provide summary information about what exceptions can be thrown out of a function, but in practice it was found to be problematic …”

Text and Source code: https://docs.microsoft.com/en-us/cpp/cpp/exception-specifications-throw-cpp?view=vs-2017

noexcept -It turns an exception throw into a call to std::terminate(). -One can make a compile-time query if the function has been declared as noexcept.

Text and Source code: https://akrzemi1.wordpress.com/2014/04/24/noexcept-what-for/

#include <iostream>
#include <functional>

using namespace std;

void test(int i) noexcept
{
	if(i == 0)
		throw 2.0;
	else
		cout << "i = " << i << endl;
}

int main()
{
	int * x = nullptr;
	try
	{
		test(10);
		test(0);
		
		x = new int();
		throw 20;
	}
	catch ( double e )
	{
		delete x;
		cout << "An exception (double) = " << e << endl;
	}
	catch ( int e )
	{
		delete x;
		cout << "An exception (int) = " << e << endl;
	}
	
	cout << "end";
	return 0;
}