| 1 |
{%MainUnit castleutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2018 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Things related to exiting the program. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Descend from this to mark exceptions that should be shown |
| 23 |
in more concise way by @link(ExceptMessage) function. |
| 24 |
It avoids showing the ClassName in output (and stack trace, in debug). |
| 25 |
|
| 26 |
Useful for exceptions that have clear @code(Message), |
| 27 |
and it is normal that they may occur during normal execution of the program |
| 28 |
and be visible to users. |
| 29 |
E.g. EInvalidParams. } |
| 30 |
EShortErrorMessage = class(Exception); |
| 31 |
EWithHiddenClassName = EShortErrorMessage deprecated 'use EShortErrorMessage'; |
| 32 |
|
| 33 |
{ @deprecated Special class to raise an exception that is always catched. } |
| 34 |
TCodeBreaker = class(TObject) |
| 35 |
end deprecated; |
| 36 |
|
| 37 |
{ Nice exception description. |
| 38 |
Contains the ClassName (unless it descends from EShortErrorMessage), |
| 39 |
and the exception Message (if it descends from Exception). } |
| 40 |
function ExceptMessage(const E: TObject): string; |
| 41 |
|
| 42 |
procedure ProgramBreak; deprecated 'Use Halt'; |
| 43 |
|
| 44 |
{$endif read_interface} |
| 45 |
|
| 46 |
{$ifdef read_implementation} |
| 47 |
|
| 48 |
function ExceptMessage(const E: TObject): string; |
| 49 |
var |
| 50 |
Message, NiceMessage: string; |
| 51 |
begin |
| 52 |
if E is Exception then |
| 53 |
begin |
| 54 |
Message := Exception(E).Message; |
| 55 |
|
| 56 |
if E is EInOutError then |
| 57 |
begin |
| 58 |
{ EInOutError message text can be improved } |
| 59 |
case EInOutError(E).ErrorCode of |
| 60 |
1..99: NiceMessage := SysErrorMessage(EInOutError(E).ErrorCode); |
| 61 |
100: NiceMessage := 'End of File'; |
| 62 |
101: NiceMessage := 'Disk Full'; |
| 63 |
102: NiceMessage := 'File variable not assigned'; |
| 64 |
103: NiceMessage := 'File not open'; |
| 65 |
104: NiceMessage := 'File not open for input'; |
| 66 |
105: NiceMessage := 'File not open for output'; |
| 67 |
106: NiceMessage := 'Error in formatted input'; |
| 68 |
107: NiceMessage := 'File already open'; |
| 69 |
else NiceMessage := ''; |
| 70 |
end; |
| 71 |
if NiceMessage <> '' then |
| 72 |
Message := Message + ' (' + NiceMessage + ')'; |
| 73 |
end; |
| 74 |
|
| 75 |
end else |
| 76 |
Message := ''; |
| 77 |
|
| 78 |
if (E is EShortErrorMessage) and (Message <> '') then |
| 79 |
begin |
| 80 |
Result := Message; |
| 81 |
end else |
| 82 |
begin |
| 83 |
Result := 'Exception "' + E.ClassName + '"'; |
| 84 |
if Message <> '' then |
| 85 |
Result := Result + ':' + NL + Message; |
| 86 |
end; |
| 87 |
end; |
| 88 |
|
| 89 |
procedure ProgramBreak; |
| 90 |
begin |
| 91 |
Halt; |
| 92 |
end; |
| 93 |
|
| 94 |
{$endif read_implementation} |