errordemo.lpr 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. program errordemo;
  2. {$mode objfpc}
  3. uses
  4. BrowserConsole, JS, Classes, SysUtils, Web;
  5. Type
  6. { TErrorApp }
  7. TErrorApp = class
  8. function DoRaise(aEvent : TJSMouseEvent) : boolean;
  9. function DoHook(aEvent : TJSMouseEvent) : boolean;
  10. Procedure DoRaiseJS;
  11. Procedure DoRaiseJSError;
  12. Procedure DoPascalException(O : TObject);
  13. Procedure DoJSException(O : TJSObject);
  14. private
  15. procedure Run;
  16. end;
  17. function TErrorApp.DoRaise(aEvent : TJSMouseEvent) : boolean;
  18. begin
  19. Result:=False;
  20. raise exception.Create('A exception');
  21. end;
  22. function TErrorApp.DoHook(aEvent : TJSMouseEvent) : boolean;
  23. begin
  24. Result:=False;
  25. HookUncaughtExceptions;
  26. end;
  27. Procedure TErrorApp.DoPascalException(O : TObject);
  28. begin
  29. Writeln('O :',O.ClassName);
  30. if O is Exception then
  31. Writeln('Exception class message : ',Exception(O).Message);
  32. end;
  33. Procedure TErrorApp.DoJSException(O : TJSObject);
  34. begin
  35. writeln('Javascript exception: ',O.toString);
  36. if O is TJSError then
  37. Writeln('Error message : ',TJSError(O).Message);
  38. end;
  39. Procedure TErrorApp.DoRaiseJS; assembler;
  40. asm
  41. throw new Object();
  42. end;
  43. Procedure TErrorApp.DoRaiseJSError; assembler;
  44. asm
  45. var e = new Error();
  46. e.message="My error message";
  47. throw e;
  48. end;
  49. Procedure TErrorApp.Run;
  50. begin
  51. // This will only work for the main program if you have set showUncaughtExceptions before rtl.run();
  52. TJSHtmlButtonElement(Document.getElementById('btnhook')).OnClick:=@DoHook;
  53. // These will not be caught (yet)
  54. TJSHtmlButtonElement(Document.getElementById('btn')).OnClick:=@DoRaise;
  55. // Uncomment this to set default exception handlers
  56. // HookUncaughtExceptions;
  57. // Uncomment these to set special exception handlers
  58. // SetOnUnCaughtExceptionHandler(@DoPascalException);
  59. SetOnUnCaughtExceptionHandler(@DoJSException);
  60. // Various ways to raise an exception.
  61. // DoRaiseJS;
  62. // DoRaiseJSError;
  63. DoRaise(Nil);
  64. end;
  65. begin
  66. With TErrorApp.Create do
  67. Run;
  68. end.