本文整理汇总了C++中TryCatch::Exception方法的典型用法代码示例。如果您正苦于以下问题:C++ TryCatch::Exception方法的具体用法?C++ TryCatch::Exception怎么用?C++ TryCatch::Exception使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TryCatch
的用法示例。
在下文中一共展示了TryCatch::Exception方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: context_eval
// [[Rcpp::export]]
std::string context_eval(std::string src, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx){
// Test if context still exists
if(!ctx)
throw std::runtime_error("Context has been disposed.");
// Create a scope
HandleScope handle_scope;
Context::Scope context_scope(*ctx);
// Compile source code
TryCatch trycatch;
Handle<Script> script = compile_source(src);
if(script.IsEmpty()) {
Local<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
throw std::invalid_argument(*exception_str);
}
// Run the script to get the result.
Handle<Value> result = script->Run();
if(result.IsEmpty()){
Local<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
throw std::runtime_error(*exception_str);
}
// Convert result to UTF8.
String::Utf8Value utf8(result);
return *utf8;
}
示例2: context_scope
extern "C" Handle<Value> execute_string(Persistent<Context> context,
const char* s,
bool* is_exception) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Enter the created context for compiling and
// running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New(s);
// Compile it
Handle<Script> script = Script::Compile(source);
// try-catch handler
TryCatch trycatch;
// Run it
Persistent<Value> result = Persistent<Value>::New(script->Run());
// Script->Run() returns an empty handle if the code threw an exception
if (result.IsEmpty()) {
*is_exception = true;
Handle<Value> exception = trycatch.Exception();
// String::AsciiValue exception_str(exception);
return Persistent<Value>::New(exception);
}
return result;
}
示例3: exception_str
std::string V8Engine::compileScript(std::string script)
{
HandleScope handleScope;
TryCatch tc;
Local<String> source = String::New(script.c_str());
// Compile the source code.
Local<Script> code = Script::Compile(source);
if (!code.IsEmpty())
return "";
// There were errors, return them
std::string ret = "";
Handle<Object> exception = tc.Exception()->ToObject();
String::AsciiValue exception_str(exception);
ret += *exception_str; ret += "\n";
Local<Message> message = tc.Message();
ret += *(v8::String::Utf8Value( message->Get() )); ret += "\n";
ret += "Source line: "; ret += *(v8::String::Utf8Value( message->GetSourceLine() )); ret += "\n";
ret += "Source line number: "; ret += Utility::toString(message->GetLineNumber()); ret += "\n";
return ret;
}
示例4:
void Susi::JS::Engine::RegisterProcessor(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() < 2) return;
Handle<Value> callbackValue = args[1];
if(callbackValue->IsFunction()){
Handle<Value> topicValue = args[0];
std::string topic{Susi::JS::Engine::convertFromJS(topicValue).toString()};
std::shared_ptr<Persistent<Function>> jsCallback{new Persistent<Function>(Isolate::GetCurrent(),Handle<Function>::Cast(callbackValue))};
Susi::Events::Processor callback = [jsCallback](Susi::Events::EventPtr event){
Local<Function> func = Local<Function>::New(Isolate::GetCurrent(),*jsCallback);
Handle<Value> callbackArguments[1];
callbackArguments[0] = Susi::JS::Engine::convertFromCPP(event->toAny());
TryCatch trycatch;
auto res = func->Call(func,1,callbackArguments);
if (res.IsEmpty()) {
Handle<Value> exception = trycatch.Exception();
String::Utf8Value exception_str(exception);
std::cout<<*exception_str<<std::endl;
}
};
long id = Susi::JS::engine->susi_client.subscribe(topic,callback);
args.GetReturnValue().Set((double)id);
}else{
args.GetReturnValue().Set(false);
}
}
示例5: wWinMain
int __stdcall wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){
int rt = -1;
initContext();
HandleScope store;
runJSRes(IDR_JS_STRUCT,L"app.lib");
Handle<Value> result = runJSFile(L"main.js",L"utf-8");
if(!result.IsEmpty()){//只有出错的时候才会返回Empty, 否则即使没有返回值, result仍然是Undefined.
Local<Object> gObj = getGlobal();
Local<Function> main = GetJSVariant<Function>(gObj,L"main");
if(main.IsEmpty()){
//InnerMsg(L"没有发现 main 函数",MT_ERROR);
}else if(!main->IsFunction()){
//InnerMsg(L"main 不是函数",MT_ERROR);
}else{
TryCatch err;
Handle<Value> args[1];
args[0] = String::New((uint16_t*)lpCmdLine);
Local<Value> r = main->Call(gObj,1,args);
if(!err.Exception().IsEmpty()){
ReportError(err);
}else
rt = r->Int32Value();
}
}
releaseContext();
return rt;
}
示例6: GetException
std::string GetException(TryCatch &try_catch, result_t hr)
{
if (try_catch.HasCaught())
{
v8::String::Utf8Value exception(try_catch.Exception());
v8::Local<v8::Message> message = try_catch.Message();
if (message.IsEmpty())
return ToCString(exception);
else
{
v8::Local<v8::Value> trace_value = try_catch.StackTrace();
if (!IsEmpty(trace_value))
{
v8::String::Utf8Value stack_trace(trace_value);
return ToCString(stack_trace);
}
std::string strError;
v8::String::Utf8Value filename(message->GetScriptResourceName());
if (qstrcmp(ToCString(exception), "SyntaxError: ", 13))
{
strError.append(ToCString(exception));
strError.append("\n at ");
}
else
{
strError.append((ToCString(exception) + 13));
strError.append("\n at ");
}
strError.append(ToCString(filename));
int lineNumber = message->GetLineNumber();
if (lineNumber > 0)
{
char numStr[32];
strError.append(1, ':');
sprintf(numStr, "%d", lineNumber);
strError.append(numStr);
strError.append(1, ':');
sprintf(numStr, "%d", message->GetStartColumn() + 1);
strError.append(numStr);
}
return strError;
}
}
else if (hr < 0)
return getResultMessage(hr);
return "";
}
示例7: done
void JSHandler::done() {
if (!done_cb.IsEmpty()) {
Local<Value> argv[0] = { };
TryCatch trycatch;
Handle<Value> v = done_cb->Call(Context::GetCurrent()->Global(), 0, argv);
if (v.IsEmpty()) {
Handle<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
printf("Exception: %s\n", *exception_str);
exit(1);
}
}
}
示例8: InvokeCallback
void CoreClrNodejsFuncInvokeContext::InvokeCallback(void* data)
{
DBG("CoreClrNodejsFuncInvokeContext::InvokeCallback");
CoreClrNodejsFuncInvokeContext* context = (CoreClrNodejsFuncInvokeContext*) data;
v8::Local<v8::Value> v8Payload = CoreClrFunc::MarshalCLRToV8(context->Payload, context->PayloadType);
static Nan::Persistent<v8::Function> callbackFactory;
static Nan::Persistent<v8::Function> callbackFunction;
Nan::HandleScope scope;
// See https://github.com/tjanczuk/edge/issues/125 for context
if (callbackFactory.IsEmpty())
{
v8::Local<v8::Function> v8FuncCallbackFunction = Nan::New<v8::FunctionTemplate>(coreClrV8FuncCallback)->GetFunction();
callbackFunction.Reset(v8FuncCallbackFunction);
v8::Local<v8::String> code = Nan::New<v8::String>(
"(function (cb, ctx) { return function (e, d) { return cb(e, d, ctx); }; })").ToLocalChecked();
v8::Local<v8::Function> callbackFactoryFunction = v8::Local<v8::Function>::Cast(v8::Script::Compile(code)->Run());
callbackFactory.Reset(callbackFactoryFunction);
}
v8::Local<v8::Value> factoryArgv[] = { Nan::New(callbackFunction), Nan::New<v8::External>((void*)context) };
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(
Nan::New(callbackFactory)->Call(Nan::GetCurrentContext()->Global(), 2, factoryArgv));
v8::Local<v8::Value> argv[] = { v8Payload, callback };
TryCatch tryCatch;
DBG("CoreClrNodejsFuncInvokeContext::InvokeCallback - Calling JavaScript function");
Nan::Call(Nan::New(*(context->FunctionContext->Func)), Nan::GetCurrentContext()->Global(), 2, argv);
DBG("CoreClrNodejsFuncInvokeContext::InvokeCallback - Called JavaScript function");
if (tryCatch.HasCaught())
{
DBG("CoreClrNodejsFuncInvokeContext::InvokeCallback - Caught JavaScript exception");
void* exceptionData;
CoreClrFunc::MarshalV8ExceptionToCLR(tryCatch.Exception(), &exceptionData);
DBG("CoreClrNodejsFuncInvokeContext::InvokeCallback - Exception message is: %s", (char*)exceptionData);
context->Complete(TaskStatusFaulted, exceptionData, V8TypeException);
}
else
{
// Kick the next tick
CallbackHelper::KickNextTick();
}
}
示例9: handleException
void handleException(TryCatch& tc)
{
Logging::log(Logging::ERROR, "V8 exception:\r\n");
HandleScope handleScope;
Handle<Object> exception = tc.Exception()->ToObject();
String::AsciiValue exception_str(exception);
Logging::log(Logging::ERROR, " : %s\r\n", *exception_str);
/*
Handle<Array> names = exception->GetPropertyNames();
for (unsigned int i = 0; i < names->Length(); i++)
{
std::string strI = Utility::toString((int)i);
Logging::log(Logging::ERROR, " %d : %s : %s\r\n", i,
*(v8::String::Utf8Value(names->Get(String::New(strI.c_str()))->ToString())),
*(v8::String::Utf8Value(exception->Get(names->Get(String::New(strI.c_str()))->ToString())->ToString()))
);
}
*/
Local<Message> message = tc.Message();
Logging::log(Logging::ERROR, "Message: Get: %s\r\n", *(v8::String::Utf8Value( message->Get() )));
Logging::log(Logging::ERROR, "Message: GetSourceLine: %s\r\n", *(v8::String::Utf8Value( message->GetSourceLine() )));
Logging::log(Logging::ERROR, "Message: GetScriptResourceName: %s\r\n", *(v8::String::Utf8Value( message->GetScriptResourceName()->ToString() )));
Logging::log(Logging::ERROR, "Message: GetLineNumber: %d\r\n", message->GetLineNumber() );
Local<Value> stackTrace = tc.StackTrace();
if (!stackTrace.IsEmpty())
{
Logging::log(Logging::ERROR, "Stack trace: %s\r\n", *(v8::String::Utf8Value( stackTrace->ToString() )));
printf("\r\n\r\n^Stack trace^: %s\r\n", *(v8::String::Utf8Value( stackTrace->ToString() )));
}
else
Logging::log(Logging::ERROR, "No stack trace available in C++ handler (see above for possible in-script stack trace)\r\n");
#ifdef SERVER
std::string clientMessage = *(v8::String::Utf8Value( message->Get() ));
clientMessage += " - ";
clientMessage += *(v8::String::Utf8Value( message->GetSourceLine() ));
ServerSystem::fatalMessageToClients(clientMessage);
#endif
// assert(0);
throw ScriptException("Bad!");
}
示例10: jserror
jsvalue JsEngine::ErrorFromV8(TryCatch& trycatch)
{
jsvalue v;
HandleScope scope;
Local<Value> exception = trycatch.Exception();
v.type = JSVALUE_TYPE_UNKNOWN_ERROR;
v.value.str = 0;
v.length = 0;
// If this is a managed exception we need to place its ID inside the jsvalue
// and set the type JSVALUE_TYPE_MANAGED_ERROR to make sure the CLR side will
// throw on it.
if (exception->IsObject()) {
Local<Object> obj = Local<Object>::Cast(exception);
if (obj->InternalFieldCount() == 1) {
Local<External> wrap = Local<External>::Cast(obj->GetInternalField(0));
ManagedRef* ref = (ManagedRef*)wrap->Value();
v.type = JSVALUE_TYPE_MANAGED_ERROR;
v.length = ref->Id();
return v;
}
}
jserror *error = new jserror();
memset(error, 0, sizeof(jserror));
Local<Message> message = trycatch.Message();
if (!message.IsEmpty()) {
error->line = message->GetLineNumber();
error->column = message->GetStartColumn();
error->resource = AnyFromV8(message->GetScriptResourceName());
error->message = AnyFromV8(message->Get());
}
if (exception->IsObject()) {
Local<Object> obj2 = Local<Object>::Cast(exception);
error->type = AnyFromV8(obj2->GetConstructorName());
}
error->exception = AnyFromV8(exception);
v.type = JSVALUE_TYPE_ERROR;
v.value.ptr = error;
return v;
}
示例11: set_perl_error
void set_perl_error(const TryCatch& try_catch) {
Handle<Message> msg = try_catch.Message();
char message[1024];
snprintf(
message,
1024,
"%s at %s:%d",
*(String::Utf8Value(try_catch.Exception())),
!msg.IsEmpty() ? *(String::AsciiValue(msg->GetScriptResourceName())) : "EVAL",
!msg.IsEmpty() ? msg->GetLineNumber() : 0
);
sv_setpv(ERRSV, message);
sv_utf8_upgrade(ERRSV);
}
示例12: filename
void V8Util::reportException(TryCatch &tryCatch, bool showLine)
{
HandleScope scope;
Handle<Message> message = tryCatch.Message();
if (nameSymbol.IsEmpty()) {
nameSymbol = SYMBOL_LITERAL("name");
messageSymbol = SYMBOL_LITERAL("message");
}
if (showLine) {
Handle<Message> message = tryCatch.Message();
if (!message.IsEmpty()) {
String::Utf8Value filename(message->GetScriptResourceName());
String::Utf8Value msg(message->Get());
int linenum = message->GetLineNumber();
LOGE(EXC_TAG, "Exception occurred at %s:%i: %s", *filename, linenum, *msg);
}
}
Local<Value> stackTrace = tryCatch.StackTrace();
String::Utf8Value trace(tryCatch.StackTrace());
if (trace.length() > 0 && !stackTrace->IsUndefined()) {
LOGD(EXC_TAG, *trace);
} else {
Local<Value> exception = tryCatch.Exception();
if (exception->IsObject()) {
Handle<Object> exceptionObj = exception->ToObject();
Handle<Value> message = exceptionObj->Get(messageSymbol);
Handle<Value> name = exceptionObj->Get(nameSymbol);
if (!message->IsUndefined() && !name->IsUndefined()) {
String::Utf8Value nameValue(name);
String::Utf8Value messageValue(message);
LOGE(EXC_TAG, "%s: %s", *nameValue, *messageValue);
}
} else {
String::Utf8Value error(exception);
LOGE(EXC_TAG, *error);
}
}
}
示例13: throwSyntaxError
result_t throwSyntaxError(TryCatch &try_catch)
{
v8::String::Utf8Value exception(try_catch.Exception());
v8::Local<v8::Message> message = try_catch.Message();
if (message.IsEmpty())
ThrowError(ToCString(exception));
else
{
std::string strError;
v8::String::Utf8Value filename(message->GetScriptResourceName());
if (qstrcmp(ToCString(exception), "SyntaxError: ", 13))
{
strError.append(ToCString(exception));
strError.append("\n at ");
}
else
{
strError.append((ToCString(exception) + 13));
strError.append("\n at ");
}
strError.append(ToCString(filename));
int lineNumber = message->GetLineNumber();
if (lineNumber > 0)
{
char numStr[32];
strError.append(1, ':');
sprintf(numStr, "%d", lineNumber);
strError.append(numStr);
strError.append(1, ':');
sprintf(numStr, "%d", message->GetStartColumn() + 1);
strError.append(numStr);
}
return Runtime::setError(strError);
}
return CALL_E_JAVASCRIPT;
}
示例14: ReportError
int ReportError(TryCatch& try_catch){
cs::String str;
HandleScope handle_scope;
v8::String::Value exception(try_catch.Exception());
const wchar_t* exception_string = ToCString(exception);
Handle<v8::Message> message = try_catch.Message();
if (message.IsEmpty()) {
str.Format(L"%s\n",exception_string);
} else {
cs::String buf;
v8::String::Value filename(message->GetScriptResourceName());
const wchar_t* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
buf.Format(L"file:\t%s\r\nline:\t%i\r\n%s\r\n\r\n", filename_string, linenum, exception_string);
str += buf;
v8::String::Value sourceline(message->GetSourceLine());
const wchar_t* sourceline_string = ToCString(sourceline);
buf.Format(L"%s", sourceline_string);
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
//str += L" ";
}
buf.Insert('^',start);
buf.Trim();
str += buf;
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
//str += L"^";
}
/*str += L"\n";
v8::String::Value stack_trace(try_catch.StackTrace());
if (stack_trace.length() > 0) {
const wchar_t* stack_trace_string = ToCString(stack_trace);
buf.Format(L"%s\n", stack_trace_string);
str += buf;
}*/
}
return MessageBox(0,str,L"Error",MB_ICONERROR);
}
示例15: js_eval
char* js_eval(UDF_INIT *initid, UDF_ARGS* args, char *result_buf, unsigned long *res_length, char *null_value, char *error) {
HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
//fprintf(stderr, "[mylog] input: %s\n", (char*)(args->args[0]));
Handle<String> source = String::New((char*)(args->args[0]));
Handle<Script> script = Script::Compile(source);
TryCatch trycatch;
Handle<Value> result = script->Run();
if ( result.IsEmpty() ) {
Handle<Value> exception = trycatch.Exception();
String::AsciiValue exception_str(exception);
//fprintf(stderr, "[mylog] exception: %s\n", *exception_str);
strcpy(result_buf, *exception_str);
*res_length = strlen(*exception_str);
}
else {
String::AsciiValue ascii(result);
//fprintf(stderr, "[mylog] output: %s\n", *ascii);
strcpy(result_buf, *ascii);
*res_length = strlen(*ascii);
}
context.Dispose();
return result_buf;
}