Exception.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package haxe;
  2. import java.NativeArray;
  3. import java.lang.Throwable;
  4. import java.lang.RuntimeException;
  5. import java.lang.StackTraceElement;
  6. import java.io.PrintStream;
  7. import java.io.PrintWriter;
  8. @:coreApi
  9. class Exception extends NativeException {
  10. public var message(get,never):String;
  11. public var stack(get,never):CallStack;
  12. public var previous(get,never):Null<Exception>;
  13. public var native(get,never):Any;
  14. @:noCompletion var __exceptionStack:Null<CallStack>;
  15. @:noCompletion var __nativeException:Throwable;
  16. @:noCompletion var __previousException:Null<Exception>;
  17. static function caught(value:Any):Exception {
  18. if(Std.is(value, Exception)) {
  19. return value;
  20. } else if(Std.isOfType(value, Throwable)) {
  21. return new Exception((value:Throwable).getMessage(), null, value);
  22. } else {
  23. return new ValueException(value, null, value);
  24. }
  25. }
  26. static function thrown(value:Any):Any {
  27. if(Std.isOfType(value, Exception)) {
  28. var native = (value:Exception).__nativeException;
  29. return Std.isOfType(native, RuntimeException) ? native : value;
  30. } else if(Std.isOfType(value, RuntimeException)) {
  31. return value;
  32. } else if(Std.isOfType(value, Throwable)) {
  33. return new Exception((value:Throwable).getMessage(), null, value);
  34. } else {
  35. var e = new ValueException(value);
  36. var stack = e.getStackTrace();
  37. if(stack.length > 1) {
  38. e.setStackTrace(java.util.Arrays.copyOfRange(stack, 1, stack.length));
  39. }
  40. return e;
  41. }
  42. }
  43. public function new(message:String, ?previous:Exception, ?native:Any) {
  44. super(message, cast previous);
  45. __previousException = previous;
  46. if(native != null && Std.isOfType(native, Throwable)) {
  47. __nativeException = native;
  48. setStackTrace(__nativeException.getStackTrace());
  49. } else {
  50. __nativeException = cast this;
  51. }
  52. }
  53. function unwrap():Any {
  54. return __nativeException;
  55. }
  56. override public function toString():String {
  57. return inline CallStack.exceptionToString(this);
  58. }
  59. function get_message():String {
  60. return this.getMessage();
  61. }
  62. function get_previous():Null<Exception> {
  63. return __previousException;
  64. }
  65. final function get_native():Any {
  66. return __nativeException;
  67. }
  68. function get_stack():CallStack {
  69. return switch __exceptionStack {
  70. case null:
  71. __exceptionStack = NativeStackTrace.toHaxe(__nativeException.getStackTrace());
  72. case s: s;
  73. }
  74. }
  75. }
  76. @:dox(hide)
  77. @:noCompletion
  78. @:native('java.lang.RuntimeException')
  79. private extern class NativeException {
  80. @:noCompletion private function new(?message:String, ?cause:Throwable):Void;
  81. @:noCompletion @:skipReflection private function addSuppressed (param1:Throwable):Void;
  82. @:noCompletion @:skipReflection private function fillInStackTrace ():Throwable;
  83. @:noCompletion @:skipReflection private function getCause ():Throwable;
  84. @:noCompletion @:skipReflection private function getLocalizedMessage ():String;
  85. @:noCompletion @:skipReflection private function getMessage ():String;
  86. @:noCompletion @:skipReflection private function getStackTrace ():NativeArray<StackTraceElement>;
  87. @:noCompletion @:skipReflection private function getSuppressed ():NativeArray<Throwable>;
  88. @:noCompletion @:skipReflection private function initCause (param1:Throwable):Throwable;
  89. @:noCompletion @:skipReflection @:overload private function printStackTrace (param1:PrintWriter):Void;
  90. @:noCompletion @:skipReflection @:overload private function printStackTrace ():Void;
  91. @:noCompletion @:skipReflection @:overload private function printStackTrace (param1:PrintStream):Void;
  92. @:noCompletion @:skipReflection private function setStackTrace (param1:NativeArray<StackTraceElement>):Void;
  93. @:noCompletion @:skipReflection private function toString ():String;
  94. }