Exception.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package haxe;
  2. import cs.system.Exception as CsException;
  3. import cs.system.diagnostics.StackTrace;
  4. @:coreApi
  5. class Exception extends NativeException {
  6. public var message(get,never):String;
  7. public var stack(get,never):CallStack;
  8. public var previous(get,never):Null<Exception>;
  9. public var native(get,never):Any;
  10. @:noCompletion var __exceptionStack:Null<CallStack>;
  11. @:noCompletion var __nativeStack:StackTrace;
  12. @:noCompletion var __ownStack:Bool;
  13. @:noCompletion @:ifFeature("haxe.Exception.get_stack") var __skipStack:Int = 0;
  14. @:noCompletion var __nativeException:CsException;
  15. @:noCompletion var __previousException:Null<Exception>;
  16. static public function caught(value:Any):Exception {
  17. if(Std.is(value, Exception)) {
  18. return value;
  19. } else if(Std.isOfType(value, CsException)) {
  20. return new Exception((value:CsException).Message, null, value);
  21. } else {
  22. return new ValueException(value, null, value);
  23. }
  24. }
  25. static public function thrown(value:Any):Any {
  26. if(Std.isOfType(value, Exception)) {
  27. return (value:Exception).native;
  28. } else if(Std.isOfType(value, CsException)) {
  29. return value;
  30. } else {
  31. var e = new ValueException(value);
  32. e.__shiftStack();
  33. return e;
  34. }
  35. }
  36. public function new(message:String, ?previous:Exception, ?native:Any) {
  37. super(message, previous);
  38. this.__previousException = previous;
  39. if(native != null && Std.isOfType(native, CsException)) {
  40. __nativeException = native;
  41. if(__nativeException.StackTrace == null) {
  42. __nativeStack = new StackTrace(1, true);
  43. __ownStack = true;
  44. } else {
  45. __nativeStack = new StackTrace(__nativeException, true);
  46. __ownStack = false;
  47. }
  48. } else {
  49. __nativeException = cast this;
  50. __nativeStack = new StackTrace(1, true);
  51. __ownStack = true;
  52. }
  53. }
  54. public function unwrap():Any {
  55. return __nativeException;
  56. }
  57. public function toString():String {
  58. return inline CallStack.exceptionToString(this);
  59. }
  60. @:noCompletion
  61. @:ifFeature("haxe.Exception.get_stack")
  62. inline function __shiftStack():Void {
  63. if(__ownStack) __skipStack++;
  64. }
  65. function get_message():String {
  66. return this.Message;
  67. }
  68. function get_previous():Null<Exception> {
  69. return __previousException;
  70. }
  71. final function get_native():Any {
  72. return __nativeException;
  73. }
  74. function get_stack():CallStack {
  75. return switch __exceptionStack {
  76. case null:
  77. __exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
  78. case s: s;
  79. }
  80. }
  81. }
  82. @:dox(hide)
  83. @:nativeGen
  84. @:noCompletion
  85. @:native('System.Exception')
  86. private extern class NativeException {
  87. @:noCompletion private function new(message:String, innerException:NativeException):Void;
  88. @:noCompletion @:skipReflection private final Data:cs.system.collections.IDictionary;
  89. @:noCompletion @:skipReflection private var HelpLink:String;
  90. @:noCompletion @:skipReflection private final InnerException:cs.system.Exception;
  91. @:noCompletion @:skipReflection private final Message:String;
  92. @:noCompletion @:skipReflection private var Source:String;
  93. @:noCompletion @:skipReflection private final StackTrace:String;
  94. @:noCompletion @:skipReflection private final TargetSite:cs.system.reflection.MethodBase;
  95. @:overload @:noCompletion @:skipReflection private function GetBaseException():cs.system.Exception;
  96. @:overload @:noCompletion @:skipReflection private function GetObjectData(info:cs.system.runtime.serialization.SerializationInfo, context:cs.system.runtime.serialization.StreamingContext):Void;
  97. @:overload @:noCompletion @:skipReflection private function GetType():cs.system.Type;
  98. @:overload @:noCompletion @:skipReflection private function ToString():cs.system.String;
  99. }