Exception.hx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package haxe;
  2. @:coreApi
  3. class Exception {
  4. public var message(get,never):String;
  5. public var stack(get,never):CallStack;
  6. public var previous(get,never):Null<Exception>;
  7. public var native(get,never):Any;
  8. @:noCompletion var __exceptionMessage:String;
  9. @:noCompletion var __exceptionStack:Null<CallStack>;
  10. @:noCompletion var __nativeStack:Array<String>;
  11. @:noCompletion @:ifFeature("haxe.Exception.get_stack") var __skipStack:Int = 0;
  12. @:noCompletion var __nativeException:Any;
  13. @:noCompletion var __previousException:Null<Exception>;
  14. static function caught(value:Any):Exception {
  15. if(Std.isOfType(value, Exception)) {
  16. return value;
  17. } else {
  18. return new ValueException(value, null, value);
  19. }
  20. }
  21. static function thrown(value:Any):Any {
  22. if(Std.isOfType(value, Exception)) {
  23. return (value:Exception).native;
  24. } else {
  25. var e = new ValueException(value);
  26. e.__shiftStack();
  27. return e;
  28. }
  29. }
  30. public function new(message:String, ?previous:Exception, ?native:Any) {
  31. __exceptionMessage = message;
  32. __previousException = previous;
  33. if(native != null) {
  34. __nativeException = native;
  35. __nativeStack = NativeStackTrace.exceptionStack();
  36. } else {
  37. __nativeException = this;
  38. __nativeStack = NativeStackTrace.callStack();
  39. __skipStack = 1;
  40. }
  41. }
  42. function unwrap():Any {
  43. return __nativeException;
  44. }
  45. @:keep // required for uncaught error handling
  46. public function toString():String {
  47. return message;
  48. }
  49. public function details():String {
  50. return inline CallStack.exceptionToString(this);
  51. }
  52. @:noCompletion
  53. @:ifFeature("haxe.Exception.get_stack")
  54. inline function __shiftStack():Void {
  55. __skipStack++;
  56. }
  57. function get_message():String {
  58. return __exceptionMessage;
  59. }
  60. function get_previous():Null<Exception> {
  61. return __previousException;
  62. }
  63. final function get_native():Any {
  64. return __nativeException;
  65. }
  66. function get_stack():CallStack {
  67. return switch __exceptionStack {
  68. case null:
  69. __exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
  70. case s: s;
  71. }
  72. }
  73. }