javascript中的引用错误并尝试捕获
try-catch用于对后端服务的调用,因为它可能返回空响应 当响应为空时,将标记错误: “ReferenceError:未定义modelInfo” API可能已关闭且发生引用错误这一事实是意料之中的 为了处理这个错误,代码被包装在一个try-catch中 我对try-catch的预期行为是,它会在请求服务信息时出错,然后跳转到“catch”,运行该代码 捕获代码没有运行javascript中的引用错误并尝试捕获,javascript,try-catch,Javascript,Try Catch,try-catch用于对后端服务的调用,因为它可能返回空响应 当响应为空时,将标记错误: “ReferenceError:未定义modelInfo” API可能已关闭且发生引用错误这一事实是意料之中的 为了处理这个错误,代码被包装在一个try-catch中 我对try-catch的预期行为是,它会在请求服务信息时出错,然后跳转到“catch”,运行该代码 捕获代码没有运行 var activeMessage = view.querySelector('#activeMessa
var activeMessage = view.querySelector('#activeMessage');
var html = '';
try {
//This call to the backend could throw a ReferenceError
ApiClient.getJSON(ApiClient.getUrl("VeraModelInfo")).then(function (modelInfo) {
html += modelInfo.Name;
html += '<br />';
html += modelInfo.InternalIp;
html += '<br />';
html += '<span id="icnConnectedContainer" style="color:rgb(82,181,75);font-size: 73%; opacity:0;">';
html += '<i class="md-icon" style="padding-bottom: 0.5%;">';
html += 'check_circle_outline';
html += '</i>';
html += ' Connected';
html += '</span > ';
activeMessage.style.opacity = 0;
activeMessage.innerHTML = html;
activeMessage.style.color = '#303030';
}
catch (error) { //This catch doesn't run after ReferenceError
html += '<span style="color: red" >';
html += '<i class="md-icon">';
html += 'circle_remove';
html += '</i>';
html += '<span>';
html += ' No Vera Home Automation Device Detected!';
activeMessage.innerHTML = html;
activeMessage.style.color = 'red';
activeMessage.style.opacity = 1;
activeMessage.style.display = 'block';
view.querySelector('#deviceModel').style.display = "none";
}
var activeMessage=view.querySelector('#activeMessage');
var html='';
试一试{
//此对后端的调用可能引发ReferenceError
getJSON(ApiClient.getUrl(“VeraModelInfo”))。然后(函数(modelInfo){
html+=modelnfo.Name;
html+='
';
html+=modelnfo.InternalIp;
html+='
';
html+='';
html+='';
html+=“检查圆和轮廓”;
html+='';
html+=‘已连接’;
html+='';
activeMessage.style.opacity=0;
activeMessage.innerHTML=html;
activeMessage.style.color='#303030';
}
catch(error){//此catch在ReferenceError之后不运行
html+='';
html+='';
html+=‘圈_删除’;
html+='';
html+='';
html+=“未检测到Vera家庭自动化设备!”;
activeMessage.innerHTML=html;
activeMessage.style.color='red';
activeMessage.style.opacity=1;
activeMessage.style.display='block';
view.querySelector(“#deviceModel”).style.display=“无”;
}
意识到错误并不总是例外
当请求中存在引用错误时,如何引发异常以运行捕获代码
或者识别ReferenceError以在另一个函数中处理客户机布局代码?我最终(感谢@Pointy的建议)编写了这篇文章,并掌握了Promissions的概念,这很有效
var activeMessage = view.querySelector('#activeMessage');
var html = '';
return new Promise(() => {
ApiClient.getJSON(ApiClient.getUrl("VeraModelInfo")).then(function (modelInfo) {
html += modelInfo.Name;
html += '<br />';
html += modelInfo.InternalIp;
html += '<br />';
html += '<span id="icnConnectedContainer" style="color:rgb(82,181,75);font-size: 73%; opacity:0;">';
html += '<i class="md-icon" style="padding-bottom: 0.5%;">';
html += 'check_circle_outline';
html += '</i>';
html += ' Connected';
html += '</span > ';
activeMessage.style.opacity = 0;
activeMessage.innerHTML = html;
activeMessage.style.color = '#303030';
activeMessage.animate({
transform : ['translateX(-200px)', 'translateX(0)'],
opacity : [0, 1]
},
{
duration: 300,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 525
});
var icnConnectedContainer = view.querySelector('#icnConnectedContainer');
icnConnectedContainer.animate({
opacity : [0, 1]
},
{
duration: 320,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 880
});
// Place the image for the users MiCasaVerde Device in an image.
var deviceImg = view.querySelector('#deviceModel');
deviceImg.src = modelInfo.ImageUrl;
deviceImg.style.opacity = 0;
deviceImg.style.display = 'block';
deviceImg.animate({
transform : ['scale(0.89)', 'scale(1)'],
opacity : [0, 1]
},
{
duration: 220,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 120
});
if (config.SavedDeviceProfiles) {
config.SavedDeviceProfiles.forEach(function (device) {
view.querySelector('#clientProfiles').innerHTML += (getClientHtml(device));
});
}
}, () => {
html += '<span style="color: red" >';
html += '<i class="md-icon">';
html += 'error';
html += '</i>';
html += '<span>';
html += ' No Vera Home Automation Device Detected!';
activeMessage.innerHTML = html;
activeMessage.style.color = 'red';
activeMessage.style.opacity = 1;
activeMessage.style.display = 'block';
view.querySelector('#deviceModel').style.display = "none";
});
var-activeMessage=view.querySelector('#activeMessage');
var html='';
返回新承诺(()=>{
getJSON(ApiClient.getUrl(“VeraModelInfo”))。然后(函数(modelInfo){
html+=modelnfo.Name;
html+='
';
html+=modelnfo.InternalIp;
html+='
';
html+='';
html+='';
html+=“检查圆和轮廓”;
html+='';
html+=‘已连接’;
html+='';
activeMessage.style.opacity=0;
activeMessage.innerHTML=html;
activeMessage.style.color='#303030';
activeMessage.animate({
转换:['translateX(-200px)''translateX(0)',
不透明度:[0,1]
},
{
持续时间:300,
填写:'向前',
缓和:“立方贝塞尔(0.42,0,0.58,1)”,
延误:525
});
var icnConnectedContainer=view.querySelector(“#icnConnectedContainer”);
icnConnectedContainer.animate({
不透明度:[0,1]
},
{
持续时间:320,
填写:'向前',
缓和:“立方贝塞尔(0.42,0,0.58,1)”,
延迟:880
});
//将用户micasaverede设备的图像放置在图像中。
var deviceImg=view.querySelector(“#deviceModel”);
deviceImg.src=modelInfo.ImageUrl;
deviceImg.style.opacity=0;
deviceImg.style.display='block';
设备动画({
变换:[“比例(0.89)”,“比例(1)”,
不透明度:[0,1]
},
{
持续时间:220,
填写:'向前',
缓和:“立方贝塞尔(0.42,0,0.58,1)”,
延误:120
});
if(config.SavedDeviceProfiles){
config.saveddeviceprofile.forEach(函数(设备){
view.querySelector(“#clientProfiles”).innerHTML+=(getClientHtml(设备));
});
}
}, () => {
html+='';
html+='';
html+='错误';
html+='';
html+='';
html+=“未检测到Vera家庭自动化设备!”;
activeMessage.innerHTML=html;
activeMessage.style.color='red';
activeMessage.style.opacity=1;
activeMessage.style.display='block';
view.querySelector(“#deviceModel”).style.display=“无”;
});
我最终(感谢@Pointy的建议)写了这篇文章,并掌握了承诺的概念,这很有效
var activeMessage = view.querySelector('#activeMessage');
var html = '';
return new Promise(() => {
ApiClient.getJSON(ApiClient.getUrl("VeraModelInfo")).then(function (modelInfo) {
html += modelInfo.Name;
html += '<br />';
html += modelInfo.InternalIp;
html += '<br />';
html += '<span id="icnConnectedContainer" style="color:rgb(82,181,75);font-size: 73%; opacity:0;">';
html += '<i class="md-icon" style="padding-bottom: 0.5%;">';
html += 'check_circle_outline';
html += '</i>';
html += ' Connected';
html += '</span > ';
activeMessage.style.opacity = 0;
activeMessage.innerHTML = html;
activeMessage.style.color = '#303030';
activeMessage.animate({
transform : ['translateX(-200px)', 'translateX(0)'],
opacity : [0, 1]
},
{
duration: 300,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 525
});
var icnConnectedContainer = view.querySelector('#icnConnectedContainer');
icnConnectedContainer.animate({
opacity : [0, 1]
},
{
duration: 320,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 880
});
// Place the image for the users MiCasaVerde Device in an image.
var deviceImg = view.querySelector('#deviceModel');
deviceImg.src = modelInfo.ImageUrl;
deviceImg.style.opacity = 0;
deviceImg.style.display = 'block';
deviceImg.animate({
transform : ['scale(0.89)', 'scale(1)'],
opacity : [0, 1]
},
{
duration: 220,
fill : 'forwards',
easing : 'cubic-bezier(0.42, 0, 0.58, 1)',
delay : 120
});
if (config.SavedDeviceProfiles) {
config.SavedDeviceProfiles.forEach(function (device) {
view.querySelector('#clientProfiles').innerHTML += (getClientHtml(device));
});
}
}, () => {
html += '<span style="color: red" >';
html += '<i class="md-icon">';
html += 'error';
html += '</i>';
html += '<span>';
html += ' No Vera Home Automation Device Detected!';
activeMessage.innerHTML = html;
activeMessage.style.color = 'red';
activeMessage.style.opacity = 1;
activeMessage.style.display = 'block';
view.querySelector('#deviceModel').style.display = "none";
});
var-activeMessage=view.querySelector('#activeMessage');
var html='';
返回新承诺(()=>{
getJSON(ApiClient.getUrl(“VeraModelInfo”))。然后(函数(modelInfo){
html+=modelnfo.Name;
html+='
';
html+=modelnfo.InternalIp;
html+='
';
html+='';
html+='';
html+='检查圆\u轮廓