在Javascript中理解try..catch

Aut*_*cus 8 javascript error-handling exception-handling try-catch

我有这个尝试并抓住问题.我正在尝试重定向到另一个页面.但有时它会这样做,有时却不行.我认为问题在于尝试和捕捉.有人可以帮助我理解这一点.谢谢

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();
 }
}
Run Code Online (Sandbox Code Playgroud)

Viv*_*ath 20

我认为你的主要问题是你吞咽异常,这是非常糟糕的.这就是"它有时有效"的原因.有些事情正在抛出异常,而你正在抓住它,但之后你却没有做任何其他事情.至少我会在你的catch块中显示某种错误信息.

其他一些问题:

  • 你确定你需要那些多try..catch块?代码中的当前假设是包含在a中的每一行都try..catch独立于其他行,如果这些语句中的任何一个(或多个)出现问题,执行仍然可以继续.你确定这是你想要的吗?如果是这样,肯定有更好的方法来处理这个问题.
  • 如果语句不是彼此独立的,并且如果任何时候失败意味着执行无法继续,那么您可以将所有这些语句包装在一个try..catch块中并在其中显示错误消息catch
  • 就像我之前说过的,吞咽异常是非常糟糕的!你隐藏了这个问题而没有实现任何目标.它也使调试非常困难,因为事情将停止工作,你将不知道为什么(没有例外,没有记录,没有错误消息).当意外发生中断正常程序流的事件时,将使用例外.这是你绝对想要处理的事情.

我想你想要的就是这样:

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);
}   
Run Code Online (Sandbox Code Playgroud)

这样,如果在这些语句系列的任何地方发生异常,该catch块将处理它.

Javascript也没有真正的检查异常.你可以通过一个try块来解决它,并检查你收到的异常对象*.

扩展我之前谈到的内容,有两种处理异常的方法.第一种方式,就像我之前展示的那样,假设当异常发生时,代码处于无效/未定义状态,这意味着代码遇到了不可恢复的错误.处理异常的另一种方法是,如果您知道它是可以从中恢复的.你可以用旗帜做到这一点.所以:

try {
   doSomething();
}

catch(e) {
   error = true;
}

if(error) {
   doStuffToRecoverFromError();
}

else {
   doOtherStuff();
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,逻辑流程取决于抛出的异常.重要的是异常是可恢复的,并且取决于它是否被抛出,你做了不同的事情.

*这是一个有点人为的例子,演示了检查异常.我有两个例外叫VeryBadException并且ReallyBadException可以从两个功能(随机)被抛出.该catch块处理异常并使用instanceof运算符确定它是什么类型的异常:

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);
   }
}
Run Code Online (Sandbox Code Playgroud)