Exception.hx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public function toString():String {
  46. return message;
  47. }
  48. public function details():String {
  49. return inline CallStack.exceptionToString(this);
  50. }
  51. @:noCompletion
  52. @:ifFeature("haxe.Exception.get_stack")
  53. inline function __shiftStack():Void {
  54. __skipStack++;
  55. }
  56. function get_message():String {
  57. return __exceptionMessage;
  58. }
  59. function get_previous():Null<Exception> {
  60. return __previousException;
  61. }
  62. final function get_native():Any {
  63. return __nativeException;
  64. }
  65. function get_stack():CallStack {
  66. return switch __exceptionStack {
  67. case null:
  68. __exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
  69. case s: s;
  70. }
  71. }
  72. }