Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::current_exception(3)     C++ Standard Libary    std::current_exception(3)

NAME
       std::current_exception -	std::current_exception

Synopsis
	  Defined in header <exception>
	  std::exception_ptr current_exception() noexcept;  (since C++11)

	  If  called during exception handling (typically, in a	catch clause),
       captures	the
	  current exception object  and	 creates  an  std::exception_ptr  that
       holds either a copy
	  or  a	reference to that exception object (depending on the implemen-
       tation).	The
	  referenced object remains valid at least as long as there is an  ex-
       ception_ptr object
	  that refers to it.

	  If  the  implementation  of this function requires a call to new and
       the call	fails,
	  the returned pointer	will  hold  a  reference  to  an  instance  of
       std::bad_alloc.

	  If the implementation	of this	function requires copying the captured
       exception
	  object  and  its  copy constructor throws an exception, the returned
       pointer will hold
	  a reference to the exception thrown. If the copy constructor of  the
       thrown exception
	  object  also throws, the returned pointer may	hold a reference to an
       instance	of
	  std::bad_exception to	break the endless loop.

	  If the function is called when no exception  is  being  handled,  an
       empty
	  std::exception_ptr is	returned.

Parameters
	  (none)

Return value
	  An  instance of std::exception_ptr holding a reference to the	excep-
       tion object, or a
	  copy of the exception	object,	or to an instance of std::bad_alloc or
       to an instance
	  of std::bad_exception.

Notes
	  On the implementations that follow  Itanium  C++  ABI	 (GCC,	Clang,
       etc), exceptions	are
	  allocated  on	 the  heap  when  thrown (except for bad_alloc in some
       cases), and this
	  function simply creates the smart  pointer  referencing  the	previ-
       ously-allocated
	  object,  On MSVC, exceptions are allocated on	stack when thrown, and
       this function
	  performs the heap allocation and copies the exception	object.

Example
       // Run this code

	#include <iostream>
	#include <string>
	#include <exception>
	#include <stdexcept>

	void handle_eptr(std::exception_ptr eptr) // passing by	value is ok
	{
	    try	{
		if (eptr) {
		    std::rethrow_exception(eptr);
		}
	    } catch(const std::exception& e) {
		std::cout << "Caught exception \"" << e.what() << "\"\n";
	    }
	}

	int main()
	{
	    std::exception_ptr eptr;
	    try	{
		std::string().at(1); //	this generates an std::out_of_range
	    } catch(...) {
		eptr = std::current_exception(); // capture
	    }
	    handle_eptr(eptr);
	} // destructor	for std::out_of_range called here, when	 the  eptr  is
       destructed

Possible output:
	Caught exception "basic_string::at"

See also
	  exception_ptr	       shared  pointer type for	handling exception ob-
       jects
	  (C++11)	      (typedef)
	  rethrow_exception   throws the exception from	an std::exception_ptr
	  (C++11)	      (function)
	  make_exception_ptr  creates an std::exception_ptr from an  exception
       object
	  (C++11)	      (function	template)
	  uncaught_exception
	  uncaught_exceptions  checks  if  exception  handling is currently in
       progress
	  (removed in C++20)  (function)
	  (C++17)

http://cppreference.com		  2022.07.31	     std::current_exception(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::current_exception&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help