Understanding try..catch in Javascript

I have this problem and problem. I am trying to redirect to another page. But sometimes it happens, and sometimes it is not. I think the problem is try and trick. can someone help me figure this out. Thanks

var pg = new Object(); var da = document.all; var wo = window.opener; pg.changeHideReasonID = function(){ if(pg.hideReasonID.value == 0 && pg.hideReasonID.selectedIndex > 0){ pg.otherReason.style.backgroundColor = "ffffff"; pg.otherReason.disabled = 0; pg.otherReason.focus(); } else { pg.otherReason.style.backgroundColor = "f5f5f5"; pg.otherReason.disabled = 1; } } pg.exit = function(pid){ try { if(window.opener.hideRecordReload){ window.opener.hideRecordReload(pg.recordID, pg.recordTypeID); } else { window.opener.pg.hideRecord(pg.recordID, pg.recordTypeID); } } catch(e) {} try { window.opener.pg.hideEncounter(pg.recordID); } catch(e) {} try { window.opener.pg.hideRecordResponse(pg.hideReasonID.value == 0 ? pg.otherReason.value : pg.hideReasonID.options[pg.hideReasonID.selectedIndex].text); } catch(e) {} try { window.opener.pg.hideRecord_Response(pg.recordID, pg.recordTypeID); } catch(e) {} try { window.opener.pg.hideRecord_Response(pg.recordID, pg.recordTypeID); } catch(e) {} try { window.opener.window.parent.frames[1].pg.loadQualityMeasureRequest(); } catch(e) {} try { window.opener.pg.closeWindow(); } catch(e) {} parent.loadCenter2({reportName:'redirectedpage',patientID:pid}); parent.$.fancybox.close(); } pg.hideRecord = function(){ var pid = this.pid; pg.otherReason.value = pg.otherReason.value.trim(); if(pg.hideReasonID.selectedIndex == 0){ alert("You have not indicated your reason for hiding this record."); pg.hideReasonID.focus(); } else if(pg.hideReasonID.value == 0 && pg.hideReasonID.selectedIndex > 0 && pg.otherReason.value.length < 2){ alert("You have indicated that you wish to enter a reason\nnot on the list, but you have not entered a reason."); pg.otherReason.focus(); } else { pg.workin(1); var n = new Object(); n.noheaders = 1; n.recordID = pg.recordID; n.recordType = pg.recordType; n.recordTypeID = pg.recordTypeID; n.encounterID = request.encounterID; n.hideReasonID = pg.hideReasonID.value; n.hideReason = pg.hideReasonID.value == 0 ? pg.otherReason.value : pg.hideReasonID.options[pg.hideReasonID.selectedIndex].text; Connect.Ajax.Post("/emr/hideRecord/act_hideRecord.php", n, pg.exit(pid)); } } pg.init = function(){ pg.blocker = da.blocker; pg.hourglass = da.hourglass; pg.content = da.pageContent; pg.recordType = da.recordType.value; pg.recordID = parseInt(da.recordID.value); pg.recordTypeID = parseInt(da.recordTypeID.value); pg.information = da.information; pg.hideReasonID = da.hideReasonID; pg.hideReasonID.onchange = pg.changeHideReasonID; pg.hideReasonID.tabIndex = 1; pg.otherReason = da.otherReason; pg.otherReason.tabIndex = 2; pg.otherReason.onblur = function(){ this.value = this.value.trim(); } pg.otherReason.onfocus = function(){ this.select(); } pg.btnCancel = da.btnCancel; pg.btnCancel.tabIndex = 4; pg.btnCancel.title = "Close this window"; pg.btnCancel.onclick = function(){ //window.close(); parent.$.fancybox.close(); } pg.btnHide = da.btnHide; pg.btnHide.tabIndex = 3; pg.btnHide.onclick = pg.hideRecord; pg.btnHide.title = "Hide " + pg.recordType.toLowerCase() + " record"; document.body.onselectstart = function(){ if(event.srcElement.tagName.search(/INPUT|TEXT/i)){ return false; } } pg.workin(0); } pg.workin = function(){ var n = arguments.length ? arguments[0] : 1; pg.content.disabled = pg.hideReasonID.disabled = n; pg.blocker.style.display = pg.hourglass.style.display = n ? "block" : "none"; if(n){ pg.otherReason.disabled = 1; pg.otherReason.style.backgroundColor = "f5f5f5"; } else { pg.otherReason.disabled = !(pg.hideReasonID.value == 0 && pg.hideReasonID.selectedIndex > 0); pg.otherReason.style.backgroundColor = pg.otherReason.disabled ? "f5f5f5" : "ffffff"; pg.hideReasonID.focus(); } } 
+7
javascript exception-handling try-catch error-handling
source share
2 answers

I think your main problem is that you are swallowing exceptions that are very bad . That's why "sometimes it works." Something throws an exception, and you catch it, but then after that you do nothing. At least I would display some kind of error message in your catch .

A few other issues:

  • Are you sure you need these few try..catch blocks? The current assumption in your code is that every line enclosed in try..catch is independent of the others, and execution can continue if something goes wrong in any (or more) of these statements. Are you sure this is what you want? If so, definitely the best way to handle this.
  • If the statements are not independent of each other, and if a failure at any point means that the execution cannot continue, you can wrap all these statements in a single try..catch block and display the error message in catch
  • As I said, swallowing exceptions is very bad ! You hide the problem and do nothing. It also makes debugging extremely difficult because everything will stop working and you won’t know why (without exception, without registration, error messages). Exceptions are used when something unexpected happens that interrupts the normal flow of a program. This is what you definitely want to handle.

I think you can do this:

 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); } 

Thus, if an exception occurs anywhere in these series of statements, the catch will handle it.

Javascript also does not have true checked-exceptions. You can get around it by having one try block and checking the exception object you get * .

Extending what I mentioned earlier, there are two ways to handle exceptions. The first method, as I showed earlier, assumes that when an exception occurs, the code is in an invalid / undefined state, and this means that the code has detected a fatal error. Another way to handle exceptions is if you know that this is something that you can recover. You can do this with a flag. So:

 try { doSomething(); } catch(e) { error = true; } if(error) { doStuffToRecoverFromError(); } else { doOtherStuff(); } 

In this case, your logic flow depends on the exception thrown. The important thing is that the exception can be restored, and depending on whether it was selected or not, you do different things.

* Here is a somewhat contrived example showing proven exceptions. I have two exceptions called VeryBadException and ReallyBadException that can be selected (randomly) from two functions. The catch handles the exception and calculates what type of exception it uses with the instanceof operator):

 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); } } 
+19
source share

Good practice does something with caught exceptions.

What happens here if the error (say the page loading failed), an exception is thrown inside one of your try blocks. The corresponding catch block captures it and says, β€œThis exception has been addressed,” but you really haven't done anything with it.

Try to print (e.Message); inside your catch blocks to find out exactly what error causes the page to load, and then add code to your catch block to deal with this error.

+3
source share

All Articles