gpt4 book ai didi

javascript - 理解 Javascript 中的 try..catch

转载 作者:数据小太阳 更新时间:2023-10-29 04:00:50 28 4
gpt4 key购买 nike

我有这个 try and catch 问题。我正在尝试重定向到另一个页面。但有时会,有时不会。我认为问题在于 try and 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();
}
}

最佳答案

我认为您的主要问题是您吞下了异常,这非常糟糕。这就是为什么“它有时会起作用”的原因。某事抛出异常,您正在捕获它,但在那之后您没有做任何其他事情。至少我会在您的 catch block 中显示某种错误消息。

其他一些问题:

  • 你确定你需要那些多个 try..catch block 吗?当前代码中的假设是 try..catch 中包裹的每一行都独立于其他行,如果其中任何一个(或多个)出现问题,执行仍然可以继续声明。你确定这是你想要的吗?如果是这样,肯定有更好的方法来处理这个问题。
  • 如果这些语句彼此独立,并且如果任何一点的失败意味着执行无法继续,那么您可以将所有这些语句包装在一个单个 try..catch block 并在 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);
}

这样,如果在这一系列语句中的任何地方发生异常,catch block 将处理它。

Javascript 也没有真正的检查异常。您可以通过使用单个 try block 并检查您收到的异常对象来绕过它*

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

try {
doSomething();
}

catch(e) {
error = true;
}

if(error) {
doStuffToRecoverFromError();
}

else {
doOtherStuff();
}

在这种情况下,您的逻辑流取决于抛出的异常。重要的是异常是可恢复的,并且根据它是否被抛出,您可以做不同的事情。

*这是一个演示检查异常的有点人为的例子。我有两个异常,分别是 VeryBadExceptionReallyBadException,它们可以从两个函数中(随机地)抛出。 catch block 处理异常并通过使用 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);
}
}

关于javascript - 理解 Javascript 中的 try..catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2647171/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com