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.

Please find the updated exception handling code here:

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);
auto context = isolate->GetCurrentContext();
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()

On Tuesday, February 9, 2021 at 5:56:31 PM UTC+5:30 Jakob Kummerow wrote:

> The general pattern is that returned MaybeHandles (from any function, not 
> just JSON::Stringify) are empty when an exception has been thrown by the 
> operation. You can use a v8::TryCatch to catch this exception and inspect 
> it, it'll include a helpful error message.
>
>
> On Tue, Feb 9, 2021 at 1:21 PM 'Vinayaka Kamath' via v8-users <
> v8-u...@googlegroups.com> wrote:
>
>> Hello All,
>>
>> A call to JSON::Stringify is returning empty handle. I ensured that the 
>> parameters are valid. What are the other possible reasons? I tried 
>> retrieving the "JSON" object from the context using context->Global(), that 
>> fails and returns empty handle.  
>>
>> bool toJSON(v8::Isolate *isolate, const v8::Local<v8::Value> &object,
>> std::string &json) {
>>
>>    json.clear();
>>
>>     if (object.IsEmpty()) {    // Object is not empty
>>         return true;
>>     }
>>
>>      v8::HandleScope handle_scope(isolate);
>>      auto context = isolate->GetCurrentContext();
>>       std::cout << "Context: " << context.IsEmpty() << "\n";
>>       std::cout << "converting to string \n";
>>       v8::Local<v8::Value> result;
>>       if (!v8::JSON::Stringify(context, object).ToLocal(&result))      // 
>> This call is failing
>>            return false;
>>        json = *v8::String::Utf8Value(isolate, result);
>>        std::cout << "done converting to string \n" << json;
>>        return true;
>> }
>>
>> Any help would be very much appreciated, thanks!
>>
>> --
>>
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/cefdd882-bdce-4398-b263-77fa3fc95a1bn%40googlegroups.com.

Reply via email to