我希望在一个地方实现广义未捕获的异常处理代码。我通过使用v8::TryCatch
来实现这一目标。我注意到的一个问题是,当v8::TryCatch::ReThrow
下发生异常时,消息监听器永远不会触发。调用v8::TryCatch
似乎没有什么区别。
我的问题是,是否有可能以某种方式将异常发送到消息监听器?我希望在这个特定位置有hello-world.cc
,以便检查自己发生的错误,并将它们发送到单独的内部日志记录,同时,我想使用消息监听器发送错误到另一个位置(这个是面向客户的)。
我的最小repro示例如下(通过修改void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> data) {
printf("%s\n", __PRETTY_FUNCTION__);
}
int main(int argc, char** argv) {
// Initialize V8.
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
// Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
isolate->AddMessageListenerWithErrorLevel(
MessageHandler,
v8::Isolate::kMessageError | v8::Isolate::kMessageWarning |
v8::Isolate::kMessageInfo | v8::Isolate::kMessageDebug |
v8::Isolate::kMessageLog);
{
v8::Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope(isolate);
// Create a new context.
v8::Local<v8::Context> context = v8::Context::New(isolate);
// Enter the context for compiling and running the hello world script.
v8::Context::Scope context_scope(context);
// The message listener will fire if this is removed.
v8::TryCatch try_catch(isolate);
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "throw new Error()",
v8::NewStringType::kNormal)
.ToLocalChecked();
// Compile the source code.
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
// Run the script to get the result.
v8::Local<v8::Value> result;
if (script->Run(context).ToLocal(&result)) {
// Convert the result to an UTF8 string and print it.
v8::String::Utf8Value utf8(isolate, result);
printf("Success: %s\n", *utf8);
} else {
printf("Error\n");
// Calling this doesn't seem to make a difference.
try_catch.ReThrow();
}
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}
示例文件创建)。
@Entity
public class PasswordResetCode {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
private User user;
@Column(unique = true)
private String code;
public PasswordResetCode(User user, String code) {
this.user = user;
user.addPasswordResetCode(this);
this.code = code;
}
// ...
}
@Entity
@Table(name = "\"user\"")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
private Set<PasswordResetCode> passwordResetCodes = new HashSet<>();
public void addPasswordResetCode(PasswordResetCode passwordResetCode) {
this.passwordResetCodes.add(passwordResetCode);
}
// ...
}
只有在删除try catch后才会调用消息侦听器。
答案 0 :(得分:0)
您需要将
设置为truetry_catch.SetVerbose(true)