https://wiki.freepascal.org/index.php?title=pas2js_exceptions&feed=atom&action=historypas2js exceptions - Revision history2024-03-28T13:21:58ZRevision history for this page on the wikiMediaWiki 1.35.6https://wiki.freepascal.org/index.php?title=pas2js_exceptions&diff=138671&oldid=prevMichael at 08:05, 1 August 20202020-08-01T08:05:22Z<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 08:05, 1 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l63" >Line 63:</td>
<td colspan="2" class="diff-lineno">Line 63:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>These calls return the old exception handler, if there were any.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>These calls return the old exception handler, if there were any.</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>And if you set your own handler, ''HookUncaughtExceptions'' will be <del class="diffchange diffchange-inline">caught </del>automatically.</div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>And if you set your own handler, ''HookUncaughtExceptions'' will be <ins class="diffchange diffchange-inline">called </ins>automatically.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">= Navigation =</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">* Back to [[pas2js]]</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">* Back to [[lazarus pas2js integration]]</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del class="diffchange diffchange-inline">The hook</del></div></td><td class='diff-marker'>+</td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins class="diffchange diffchange-inline">[[Category:Pas2js]]</ins></div></td></tr>
</table>Michaelhttps://wiki.freepascal.org/index.php?title=pas2js_exceptions&diff=138670&oldid=prevMichael: Created page with "= Exception handling = == Uncaught exceptions == Pas2js translates exceptions to actual javascript exceptions. You can use the usual mechanisms ''try..finally..end'' and ''tr..."2020-08-01T08:03:08Z<p>Created page with "= Exception handling = == Uncaught exceptions == Pas2js translates exceptions to actual javascript exceptions. You can use the usual mechanisms ''try..finally..end'' and ''tr..."</p>
<p><b>New page</b></p><div>= Exception handling =<br />
<br />
== Uncaught exceptions ==<br />
Pas2js translates exceptions to actual javascript exceptions. You can use the usual mechanisms ''try..finally..end'' and ''try..except..end'' to handle them.<br />
<br />
In case you do not handle exceptions, the rtl.js runtime has several mechanisms to deal with uncaught exceptions.<br />
<br />
In case you don't handle the exception, the default behaviour of the rtl is not to do anything. If you run the program in the browser, you'll see a message in the console, but that is it.<br />
To visualize the exception, you can add the following in your html file:<br />
<pre><br />
<script type="application/javascript"><br />
rtl.showUncaughtExceptions=true;<br />
rtl.run();<br />
</script><br />
</pre> <br />
the browser will then use a ''window.alert()'' to show any uncaught exceptions. <br />
<br />
Note that this is handled by the small ''rtl.js'' runtime, the pas2js system units do not handle this (but see below).<br />
<br />
== Exceptions in event handlers ==<br />
When an exception is raised in an event handler, the rtl will not catch it. In fact, you will not see it.<br />
<br />
The way to handle this is to use the ''safecall'' calling convention on event handler types.<br />
<br />
When you do this, the compiler will create a callback that does what a normal callback does, but inserts code to catch exceptions and route them to the rtl.<br />
<br />
The event handlers in the web.pas unit are declared using this calling convention (version 1.5.1 and above only):<br />
<br />
<pre><br />
TJSRawEventHandler = reference to Procedure(Event: TJSEvent); safecall;<br />
</pre><br />
<br />
This means that the event handler will catch an exception and route it to the rtl event handling mechanism.<br />
<br />
== Hooking into exception handling ==<br />
<br />
As explained above, the default way to deal with exceptions is to show them using ''window.alert()''.<br />
<br />
It is possible to override this behaviour using the sysutils unit.<br />
<br />
<pre><br />
Type<br />
TShowExceptionHandler = Procedure (Const Msg : String);<br />
TUncaughtPascalExceptionHandler = reference to Procedure(aObject : TObject);<br />
TUncaughtJSExceptionHandler = reference to Procedure(aObject : TJSObject);<br />
<br />
// Handler to show an exception (used when showexception is called)<br />
OnShowException : TShowExceptionHandler = nil;<br />
<br />
Function SetOnUnCaughtExceptionHandler(aValue : TUncaughtPascalExceptionHandler) : TUncaughtPascalExceptionHandler;<br />
Function SetOnUnCaughtExceptionHandler(aValue : TUncaughtJSExceptionHandler) : TUncaughtJSExceptionHandler;<br />
Procedure HookUncaughtExceptions;<br />
</pre><br />
<br />
The ''HookUncaughtExceptions'' call hooks into the rtl handler for uncaught exceptions. <br />
<br />
After a call to ''HookUncaughtExceptions'' the exceptions caught by the rtl handling mechanism will be displayed using ShowException. <br />
<br />
You can set your own handlers for uncaught exceptions with ''SetOnUnCaughtExceptionHandler''. <br />
<br />
There are 2 handlers: one for every object that is an actual pascal object, one for Javascript objects such as JSError.<br />
This is because the roots of these 2 object trees are different.<br />
<br />
These calls return the old exception handler, if there were any.<br />
And if you set your own handler, ''HookUncaughtExceptions'' will be caught automatically.<br />
<br />
<br />
The hook</div>Michael