Last active
January 18, 2018 14:31
-
-
Save jpluimers/3cf26a5bcfc04b6ac28125ce20d6fd5b to your computer and use it in GitHub Desktop.
ExceptionHelper class helper for Exception that gets you a `Describe` including StackTrace if you have a library like JCL that implements handlers for Exception.GetExceptionStackInfoProc, GetStackInfoStringProc JclDebug.pas like https://github.com/project-jedi/jcl/blob/master/jcl/source/windows/JclDebug.pas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit ExceptionHelperUnit; | |
interface | |
uses | |
System.SysUtils; | |
type | |
ExceptionHelper = class helper for Exception | |
public | |
function Describe: string; | |
class procedure RaiseNotImplementedException(const aClass: TClass; const aMethodName: string); | |
class function GetStackTrace: string; | |
end; | |
implementation | |
uses | |
System.RTLConsts, | |
System.SysConst; | |
type | |
EStackTraceException = class(Exception); // EProgrammerNotFound to make it really clear this is only to be used in very limited places ?? | |
{ ExceptionHelper } | |
function ExceptionHelper.Describe: string; | |
var | |
lStackTrace: string; | |
begin | |
Result := inherited ToString(); | |
if Self is EInOutError then | |
if Result = System.RTLConsts.SInvalidFileName then | |
Result := System.SysConst.SInvalidFileName; | |
if Assigned(StackInfo) then | |
lStackTrace := StackTrace | |
else | |
lStackTrace := 'empty'; | |
Result := Format('Exception'#13#10'%s at $%p: %s'#13#10'with StackTrace'#13#10'%s', [ClassName, ExceptAddr, Result, lStackTrace]); | |
end; | |
class function ExceptionHelper.GetStackTrace: string; | |
begin | |
try | |
Result := 'Get StackTrace via Exception.'; | |
raise EStackTraceException.Create(Result) at ReturnAddress; | |
except | |
on E: EStackTraceException do | |
Result := E.StackTrace; | |
end; | |
end; | |
class procedure ExceptionHelper.RaiseNotImplementedException(const aClass: TClass; const aMethodName: string); | |
begin | |
raise ENotImplemented.CreateFmt('Method %s.%s is not implemented.', [aClass.ClassName, aMethodName]); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment