Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your main problem is that you're swallowing exceptions, which is <strong>very bad</strong>. This is why "it works sometimes". Something is throwing an exception, and you're catching it, but then you're not doing anything else after that. At the very least I would display some sort of error message in your <code>catch</code> block.</p> <p>A few other problems:</p> <ul> <li>Are you sure you need those multiple <code>try..catch</code> blocks? The current assumption in your code is that each line that is wrapped in a <code>try..catch</code> is independent of the others, and execution can still proceed if something goes wrong in any one (or more) of those statements. Are you sure this is what you want? If so, there is definitely a better way of handling this.</li> <li>If the statements are <em>not</em> independent of each other, and if a failure at any point means that execution <em>cannot</em> proceed, then you can wrap all of those statements in a single <code>try..catch</code> block and display an error message in the <code>catch</code></li> <li>Like I said before, swallowing exceptions is <strong>very bad</strong>! You're hiding the problem and not achieving anything. It also makes debugging extremely hard, because things will stop working and you will have no idea why (no exception, no logging, no error messages). Exceptions are used when something unexpected happens that interrupts normal program flow. It is something you definitely want to handle.</li> </ul> <p>I think what you want can be done this way:</p> <pre><code>try { if(window.opener.hideRecordReload){ window.opener.hideRecordReload(pg.recordID, pg.recordTypeID); } else { window.opener.pg.hideRecord(pg.recordID, pg.recordTypeID); } window.opener.pg.hideEncounter(pg.recordID); window.opener.pg.hideRecordResponse(pg.hideReasonID.value == 0 ? pg.otherReason.value : pg.hideReasonID.options[pg.hideReasonID.selectedIndex].text); window.opener.pg.hideRecord_Response(pg.recordID, pg.recordTypeID); window.opener.pg.hideRecord_Response(pg.recordID, pg.recordTypeID); window.opener.window.parent.frames[1].pg.loadQualityMeasureRequest(); window.opener.pg.closeWindow(); } catch(e) { console.log(e); } </code></pre> <p>This way, if an exception occurs anywhere along those series of statements, the <code>catch</code> block will handle it.</p> <p>Javascript also doesn't have true checked-exceptions. You can get around it by having a single try block, and inspecting the exception object that you receive<sup>*</sup>.</p> <p>Expanding on what I talked about earlier, there are two ways of handling exceptions. The first way, like I showed earlier, assumes that when an exception happens, the code is in an invalid/undefined state and this therefore means that the code encountered an unrecoverable error. Another way of handling exceptions is if you know it is something you can recover from. You can do this with a flag. So:</p> <pre><code>try { doSomething(); } catch(e) { error = true; } if(error) { doStuffToRecoverFromError(); } else { doOtherStuff(); } </code></pre> <p>In this case the flow of your logic depends on an exception being thrown. The important thing is that the exception is recoverable, and depending on whether it was thrown or not, you do different things.</p> <p><sup>*</sup>Here is a somewhat contrived example that demonstrates checked-exceptions. I have two exceptions called <code>VeryBadException</code> and <code>ReallyBadException</code> that can be thrown (randomly) from two functions. The <code>catch</code> block handles the exception and figures out what type of exception it is by using the <code>instanceof</code> operator):</p> <pre><code>function VeryBadException(message) { this.message = message; } function ReallyBadException(message) { this.message = message; } function foo() { var r = Math.floor(Math.random() * 4); if(r == 2) { throw new VeryBadException("Something very bad happened!"); } } function bar() { var r = Math.floor(Math.random() * 4); if(r == 1) { throw new ReallyBadException("Something REALLY bad happened!"); } } try { foo(); bar(); } catch(e) { if(e instanceof VeryBadException) { console.log(e.message); } else if(e instanceof ReallyBadException) { console.log(e.message); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload