Hello Jakob, thanks for the reply. I put the code in another TryCatch, but it has not been caught and there's no error. I am confused. On repeatedly running the same code more thing once, occasionally I see the required message.
std::string describeException(v8::Isolate *isolate, v8::TryCatch *try_catch) {
std::ostringstream os;
v8::HandleScope handle_scope(isolate);
v8::TryCatch trycatch(isolate);
v8::String::Utf8Value exception(isolate, try_catch->Exception());
const char *exception_string = ToCString(exception);
v8::Local<v8::Message> message = try_catch->Message();
os << "Exception: ";
os << exception_string;
os << " " << std::endl;
// Print exception location details
if (!message.IsEmpty()) {
// Print location
v8::String::Utf8Value filename(isolate,
message->GetScriptOrigin().ResourceName());
v8::Local<v8::Context> context(isolate->GetCurrentContext());
const char *filename_string = ToCString(filename);
int linenum = message->GetLineNumber(context).FromJust();
os << "Location: " << filename_string << ":" << linenum << " " << std::endl;
// Print source code
// Print line of source code.
os << "Code: " << std::endl;
v8::String::Utf8Value sourceline(
isolate, message->GetSourceLine(context).ToLocalChecked());
const char* sourceline_string = ToCString(sourceline);
os << sourceline_string << std::endl;
// Print stack trace
v8::Local<v8::Value> stack_trace_string;
if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) &&
stack_trace_string->IsString() &&
v8::Local<v8::String>::Cast(stack_trace_string)->Length() > 0) {
v8::String::Utf8Value stack_trace(isolate, stack_trace_string);
const char* stack_trace_string = ToCString(stack_trace);
os << stack_trace_string << std::endl;
}
}
if (trycatch.HasCaught()) {
v8::String::Utf8Value internal_exception(isolate, trycatch.Exception());
const char *internal_exception_string = ToCString(exception);
os << "Internal Error while trying to prepare exception string:" << std::endl;
os << internal_exception_string << std::endl;
}
return os.str();
}
I see that the exception messages are randomly blank despite me checking for try_catch.HasCaught()