reject()调用promise 回调后,Chrome控制台中会显示警告消息"Uncaught(in promise)".我不能围绕它背后的原因,也不知道如何摆脱它.
var p = new Promise((resolve, reject) => {
setTimeout(() => {
var isItFulfilled = false
isItFulfilled ? resolve('!Resolved') : reject('!Rejected')
}, 1000)
})
p.then(result => console.log(result))
p.catch(error => console.log(error))
Run Code Online (Sandbox Code Playgroud)
警告:

编辑:
我发现如果onRejected没有向.then(onResolved, onRejected)方法显式提供处理程序,JS将自动提供隐式处理程序.它看起来像这样:(err) => throw err.自动生成的处理程序将依次抛出.
参考:
如果IsCallable(onRejected)`为false,那么
让onRejected为" Thrower ".
http://www.ecma-international.org/ecma-262/6.0/index.html#sec-performpromisethen
鉴于下面的短代码示例:
...
print("1 parsing stuff");
List<dynamic> subjectjson;
try {
subjectjson = json.decode(response.body);
} on Exception catch (_) {
print("throwing new error");
throw Exception("Error on server");
}
print("2 parsing stuff");
...
Run Code Online (Sandbox Code Playgroud)
我希望在解码失败时执行 catch 块。但是,当返回错误响应时,终端会显示异常,并且不会触发 catch 和继续代码......
flutter: 1 parsing stuff
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type
'_InternalLinkedHashMap<String, dynamic>' is not a subtype of type
'List<dynamic>'
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
ANR是异常,错误还是什么?我们真的可以在一个try{} catch(){}结构中捕获它吗?
首先,免责声明:我有其他语言的经验,但我仍在学习C#的微妙之处
关于问题...我正在看一些代码,它以一种让我关注的方式使用try/catch块.当调用解析例程而不是返回错误代码时,程序员使用以下逻辑
catch (TclException e) {
throw new TclRuntimeError("unexpected TclException: " + e.Message,e);
}
Run Code Online (Sandbox Code Playgroud)
这是由调用者捕获的,它抛出相同的错误......
由调用者捕获,它抛出相同的错误......
.....被调用者捕获,抛出同样的错误...
备份大约6个级别.
我认为所有这些catch/throw块都会导致性能问题,或者这是C#下的合理实现吗?
我有一个使用数组执行某些操作的函数.当数组为空时我想拒绝它.
举个例子
myArrayFunction(){
return new Promise(function (resolve, reject) {
var a = new Array();
//some operation with a
if(a.length > 0){
resolve(a);
}else{
reject('Not found');
}
};
}
Run Code Online (Sandbox Code Playgroud)
拒绝操作发生时,我收到以下错误.可能未处理错误:未找到
但是,当调用myArrayFunction()时,我有以下catch.
handlers.getArray = function (request, reply) {
myArrayFunction().then(
function (a) {
reply(a);
}).catch(reply(hapi.error.notFound('No array')));
};
Run Code Online (Sandbox Code Playgroud)
什么是拒绝承诺,抓住拒绝和回应客户的正确方法?
谢谢.
在java中,如果我们只需要执行一个语句if或者for不需要括号.我们可以写:
if(condition)
executeSingleStatement();
Run Code Online (Sandbox Code Playgroud)
要么
for(init;condition;incr)
executeSingleStatement();
Run Code Online (Sandbox Code Playgroud)
但是在catch块的情况下为什么我们不能省略括号?为什么这不可能?
catch(Exception e)
e.printStackTrace();
Run Code Online (Sandbox Code Playgroud)
因为在大多数情况下 我们我在catch块中只有一个语句,它e.printStackTrace()在测试或记录语句时.
你知道任何情况下空挡块不是绝对的邪恶吗?
try
{
...
// What and When?
...
}
catch { }
Run Code Online (Sandbox Code Playgroud) 以下捕获块之间有什么区别?
try
{
...
}
catch
{
...
}
Run Code Online (Sandbox Code Playgroud)
和
try
{
...
}
catch(Exception)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我知道,在任何一种情况下,异常实例都不可用,但有什么我可以用另一个不可能做到的吗?
我有下面的代码,我可以打印出发生错误的fullclassname,classname,methodname.
此外,我能够打印行号,但打印的行号是变量"LineNumber"初始化的行.
如何在try块中打印出错误发生的确切LineNumber和ColumnNumber?
try
{
SQL Query
}
catch(Exception e)
{
String fullClassName = Thread.currentThread().getStackTrace()[1].getClassName();
String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
int lineNumber = Thread.currentThread().getStackTrace()[1].getLineNumber();
JOptionPane.showMessageDialog(null,fullClassName+"--"+className+"--"+methodName+"--"+lineNumber,"Error In Moving data from table1 to table2",JOptionPane.ERROR_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
输出继电器:
IPM.Shifting--Shifting--ConfirmTransfer_BActionPerformed--1138
Run Code Online (Sandbox Code Playgroud) 我注意到在catch块中使用toast时没有显示.有没有人知道如何在捕获异常时显示祝酒词?一个例子:
try {
// try to open a file
} catch (FileNotFoundException e) {
Toast.makeText(this, R.string.txt_file_not_found, Toast.LENGTH_LONG);
return; // cancel processing
}
Run Code Online (Sandbox Code Playgroud) catch-block ×10
c# ×3
exception ×3
try-catch ×3
android ×2
java ×2
javascript ×2
promise ×2
.net ×1
braces ×1
dart ×1
es6-promise ×1
flutter ×1
hapijs ×1
node.js ×1
performance ×1
show ×1
syntax ×1
toast ×1