1 // Created on: 1991-09-05
2 // Created by: Philippe COICADAN
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #ifndef _Standard_Failure_HeaderFile
18 #define _Standard_Failure_HeaderFile
20 #include <Standard_Type.hxx>
22 #include <Standard_CString.hxx>
23 #include <Standard_Transient.hxx>
24 #include <Standard_OStream.hxx>
25 #include <Standard_SStream.hxx>
27 DEFINE_STANDARD_HANDLE(Standard_Failure, Standard_Transient)
29 //! Forms the root of the entire exception hierarchy.
30 class Standard_Failure : public Standard_Transient
34 //! Creates a status object of type "Failure".
35 Standard_EXPORT Standard_Failure();
38 Standard_EXPORT Standard_Failure (const Standard_Failure& f);
40 //! Creates a status object of type "Failure".
41 //! @param theDesc [in] exception description
42 Standard_EXPORT Standard_Failure (const Standard_CString theDesc);
44 //! Creates a status object of type "Failure" with stack trace.
45 //! @param theDesc [in] exception description
46 //! @param theStackTrace [in] associated stack trace
47 Standard_EXPORT Standard_Failure (const Standard_CString theDesc,
48 const Standard_CString theStackTrace);
50 //! Assignment operator
51 Standard_EXPORT Standard_Failure& operator= (const Standard_Failure& f);
54 Standard_EXPORT ~Standard_Failure();
56 //! Prints on the stream @p theStream the exception name followed by the error message.
58 //! Note: there is a short-cut @c operator<< (Standard_OStream&, Handle(Standard_Failure)&)
59 Standard_EXPORT void Print (Standard_OStream& theStream) const;
61 //! Returns error message
62 Standard_EXPORT virtual Standard_CString GetMessageString() const;
64 //! Sets error message
65 Standard_EXPORT virtual void SetMessageString (const Standard_CString theMessage);
67 //! Returns the stack trace string
68 Standard_EXPORT virtual Standard_CString GetStackString() const;
70 //! Sets the stack trace string
71 Standard_EXPORT virtual void SetStackString (const Standard_CString theStack);
73 Standard_EXPORT void Reraise();
75 Standard_EXPORT void Reraise (const Standard_CString aMessage);
77 //! Reraises a caught exception and changes its error message.
78 Standard_EXPORT void Reraise (const Standard_SStream& aReason);
82 //! Raises an exception of type "Failure" and associates
83 //! an error message to it. The message can be printed
84 //! in an exception handler.
85 Standard_EXPORT static void Raise (const Standard_CString aMessage = "");
87 //! Raises an exception of type "Failure" and associates
88 //! an error message to it. The message can be constructed
90 Standard_EXPORT static void Raise (const Standard_SStream& aReason);
92 //! Used to construct an instance of the exception object as a handle.
93 //! Shall be used to protect against possible construction of exception object in C stack,
94 //! which is dangerous since some of methods require that object was allocated dynamically.
95 Standard_EXPORT static Handle(Standard_Failure) NewInstance (Standard_CString theMessage);
97 //! Used to construct an instance of the exception object as a handle.
98 Standard_EXPORT static Handle(Standard_Failure) NewInstance (Standard_CString theMessage,
99 Standard_CString theStackTrace);
101 //! Returns the default length of stack trace to be captured by Standard_Failure constructor;
102 //! 0 by default meaning no stack trace.
103 Standard_EXPORT static Standard_Integer DefaultStackTraceLength();
105 //! Sets default length of stack trace to be captured by Standard_Failure constructor.
106 Standard_EXPORT static void SetDefaultStackTraceLength (Standard_Integer theNbStackTraces);
110 //! Used to throw CASCADE exception from C signal handler.
111 //! On platforms that do not allow throwing C++ exceptions
112 //! from this handler (e.g. Linux), uses longjump to get to
113 //! the current active signal handler, and only then is
114 //! converted to C++ exception.
115 Standard_EXPORT void Jump();
117 DEFINE_STANDARD_RTTIEXT(Standard_Failure,Standard_Transient)
121 //! Used only if standard C++ exceptions are used.
122 //! Throws exception of the same type as this by C++ throw,
123 //! and stores current object as last thrown exception,
124 //! to be accessible by method Caught()
125 Standard_EXPORT virtual void Throw() const;
129 //! Reference-counted string,
130 //! Memory block is allocated with an extra 4-byte header (int representing number of references)
131 //! using low-level malloc() to avoid exceptions.
134 Standard_Integer Counter;
135 Standard_Character Message[1];
137 //! Return message string.
138 Standard_CString GetMessage() const { return (Standard_CString )&Message[0]; }
140 //! Allocate reference-counted message string.
141 static StringRef* allocate_message (Standard_CString theString);
143 //! Copy reference-counted message string.
144 static StringRef* copy_message (StringRef* theString);
146 //! Release reference-counted message string.
147 static void deallocate_message (StringRef* theString);
152 StringRef* myMessage;
153 StringRef* myStackTrace;
157 // =======================================================================
158 // function : operator<<
160 // =======================================================================
161 inline Standard_OStream& operator<< (Standard_OStream& theStream,
162 const Handle(Standard_Failure)& theFailure)
164 theFailure->Print (theStream);
168 // =======================================================================
169 // function : operator<<
171 // =======================================================================
172 inline Standard_OStream& operator<< (Standard_OStream& theStream,
173 const Standard_Failure& theFailure)
175 theFailure.Print (theStream);
179 #endif // _Standard_Failure_HeaderFile