libstdc++
exception
Go to the documentation of this file.
00001 // Exception Handling support header for -*- C++ -*-
00002 
00003 // Copyright (C) 1995-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of GCC.
00006 //
00007 // GCC is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 //
00012 // GCC is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file exception
00027  *  This is a Standard C++ Library header.
00028  */
00029 
00030 #ifndef __EXCEPTION__
00031 #define __EXCEPTION__
00032 
00033 #pragma GCC system_header
00034 
00035 #pragma GCC visibility push(default)
00036 
00037 #include <bits/c++config.h>
00038 #include <bits/atomic_lockfree_defines.h>
00039 
00040 extern "C++" {
00041 
00042 namespace std
00043 {
00044   /**
00045    * @defgroup exceptions Exceptions
00046    * @ingroup diagnostics
00047    *
00048    * Classes and functions for reporting errors via exception classes.
00049    * @{
00050    */
00051 
00052   /**
00053    *  @brief Base class for all library exceptions.
00054    *
00055    *  This is the base class for all exceptions thrown by the standard
00056    *  library, and by certain language expressions.  You are free to derive
00057    *  your own %exception classes, or use a different hierarchy, or to
00058    *  throw non-class data (e.g., fundamental types).
00059    */
00060   class exception
00061   {
00062   public:
00063     exception() _GLIBCXX_USE_NOEXCEPT { }
00064     virtual ~exception() _GLIBCXX_USE_NOEXCEPT;
00065 
00066     /** Returns a C-style character string describing the general cause
00067      *  of the current error.  */
00068     virtual const char* what() const _GLIBCXX_USE_NOEXCEPT;
00069   };
00070 
00071   /** If an %exception is thrown which is not listed in a function's
00072    *  %exception specification, one of these may be thrown.  */
00073   class bad_exception : public exception
00074   {
00075   public:
00076     bad_exception() _GLIBCXX_USE_NOEXCEPT { }
00077 
00078     // This declaration is not useless:
00079     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00080     virtual ~bad_exception() _GLIBCXX_USE_NOEXCEPT;
00081 
00082     // See comment in eh_exception.cc.
00083     virtual const char* what() const _GLIBCXX_USE_NOEXCEPT;
00084   };
00085 
00086   /// If you write a replacement %terminate handler, it must be of this type.
00087   typedef void (*terminate_handler) ();
00088 
00089   /// If you write a replacement %unexpected handler, it must be of this type.
00090   typedef void (*unexpected_handler) ();
00091 
00092   /// Takes a new handler function as an argument, returns the old function.
00093   terminate_handler set_terminate(terminate_handler) _GLIBCXX_USE_NOEXCEPT;
00094 
00095 #if __cplusplus >= 201103L
00096   /// Return the current terminate handler.
00097   terminate_handler get_terminate() noexcept;
00098 #endif
00099 
00100   /** The runtime will call this function if %exception handling must be
00101    *  abandoned for any reason.  It can also be called by the user.  */
00102   void terminate() _GLIBCXX_USE_NOEXCEPT __attribute__ ((__noreturn__));
00103 
00104   /// Takes a new handler function as an argument, returns the old function.
00105   unexpected_handler set_unexpected(unexpected_handler) _GLIBCXX_USE_NOEXCEPT;
00106 
00107 #if __cplusplus >= 201103L
00108   /// Return the current unexpected handler.
00109   unexpected_handler get_unexpected() noexcept;
00110 #endif
00111 
00112   /** The runtime will call this function if an %exception is thrown which
00113    *  violates the function's %exception specification.  */
00114   void unexpected() __attribute__ ((__noreturn__));
00115 
00116   /** [18.6.4]/1:  'Returns true after completing evaluation of a
00117    *  throw-expression until either completing initialization of the
00118    *  exception-declaration in the matching handler or entering @c unexpected()
00119    *  due to the throw; or after entering @c terminate() for any reason
00120    *  other than an explicit call to @c terminate().  [Note: This includes
00121    *  stack unwinding [15.2].  end note]'
00122    *
00123    *  2: 'When @c uncaught_exception() is true, throwing an
00124    *  %exception can result in a call of @c terminate()
00125    *  (15.5.1).'
00126    */
00127   bool uncaught_exception() _GLIBCXX_USE_NOEXCEPT __attribute__ ((__pure__));
00128 
00129   // @} group exceptions
00130 } // namespace std
00131 
00132 namespace __gnu_cxx
00133 {
00134 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00135 
00136   /**
00137    *  @brief A replacement for the standard terminate_handler which
00138    *  prints more information about the terminating exception (if any)
00139    *  on stderr.
00140    *
00141    *  @ingroup exceptions
00142    *
00143    *  Call
00144    *   @code
00145    *     std::set_terminate(__gnu_cxx::__verbose_terminate_handler)
00146    *   @endcode
00147    *  to use.  For more info, see
00148    *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt02ch06s02.html
00149    *
00150    *  In 3.4 and later, this is on by default.
00151    */
00152   void __verbose_terminate_handler();
00153 
00154 _GLIBCXX_END_NAMESPACE_VERSION
00155 } // namespace
00156 
00157 } // extern "C++"
00158 
00159 #pragma GCC visibility pop
00160 
00161 #if (__cplusplus >= 201103L) && (ATOMIC_INT_LOCK_FREE > 1)
00162 #include <bits/exception_ptr.h>
00163 #include <bits/nested_exception.h>
00164 #endif
00165 
00166 #endif