V8 Project
v8::TryCatch Class Reference

An external exception handler. More...

#include <v8.h>

+ Collaboration diagram for v8::TryCatch:

Public Member Functions

 TryCatch ()
 Creates a new try/catch block and registers it with v8. More...
 
 ~TryCatch ()
 Unregisters and deletes this try/catch block. More...
 
bool HasCaught () const
 Returns true if an exception has been caught by this try/catch block. More...
 
bool CanContinue () const
 For certain types of exceptions, it makes no sense to continue execution. More...
 
bool HasTerminated () const
 Returns true if an exception has been caught due to script execution being terminated. More...
 
Handle< ValueReThrow ()
 Throws the exception caught by this TryCatch in a way that avoids it being caught again by this same TryCatch. More...
 
Local< ValueException () const
 Returns the exception caught by this try/catch block. More...
 
Local< ValueStackTrace () const
 Returns the .stack property of the thrown object. More...
 
Local< v8::MessageMessage () const
 Returns the message associated with this exception. More...
 
void Reset ()
 Clears any exceptions that may have been caught by this try/catch block. More...
 
void SetVerbose (bool value)
 Set verbosity of the external exception handler. More...
 
void SetCaptureMessage (bool value)
 Set whether or not this TryCatch should capture a Message object which holds source information about where the exception occurred. More...
 

Static Public Member Functions

static void * JSStackComparableAddress (v8::TryCatch *handler)
 There are cases when the raw address of C++ TryCatch object cannot be used for comparisons with addresses into the JS stack. More...
 

Private Member Functions

void ResetInternal ()
 
 TryCatch (const TryCatch &)
 
void operator= (const TryCatch &)
 
void * operator new (size_t size)
 
void operator delete (void *, size_t)
 

Private Attributes

v8::internal::Isolateisolate_
 
v8::TryCatchnext_
 
void * exception_
 
void * message_obj_
 
void * message_script_
 
void * js_stack_comparable_address_
 
int message_start_pos_
 
int message_end_pos_
 
bool is_verbose_: 1
 
bool can_continue_: 1
 
bool capture_message_: 1
 
bool rethrow_: 1
 
bool has_terminated_: 1
 

Friends

class v8::internal::Isolate
 

Detailed Description

An external exception handler.

Definition at line 5271 of file v8.h.

Constructor & Destructor Documentation

◆ TryCatch() [1/2]

v8::TryCatch::TryCatch ( )

Creates a new try/catch block and registers it with v8.

Note that all TryCatch blocks should be stack allocated because the memory location itself is compared against JavaScript try/catch blocks.

Definition at line 1906 of file api.cc.

1907  : isolate_(i::Isolate::Current()),
1909  is_verbose_(false),
1910  can_continue_(true),
1911  capture_message_(true),
1912  rethrow_(false),
1913  has_terminated_(false) {
1914  ResetInternal();
1915  // Special handling for simulators which have a separate JS stack.
1917  reinterpret_cast<void*>(v8::internal::SimulatorStack::RegisterCTryCatch(
1920 }
bool has_terminated_
Definition: v8.h:5412
void * js_stack_comparable_address_
Definition: v8.h:5405
v8::TryCatch * next_
Definition: v8.h:5401
bool is_verbose_
Definition: v8.h:5408
v8::internal::Isolate * isolate_
Definition: v8.h:5400
bool rethrow_
Definition: v8.h:5411
bool can_continue_
Definition: v8.h:5409
bool capture_message_
Definition: v8.h:5410
void ResetInternal()
Definition: api.cc:2036
v8::TryCatch * try_catch_handler()
Definition: isolate.h:593
void RegisterTryCatchHandler(v8::TryCatch *that)
Definition: isolate.cc:244
static uintptr_t RegisterCTryCatch(uintptr_t try_catch_address)
Definition: simulator-arm.h:51
DISABLE_ASAN uintptr_t GetCurrentStackPosition()
Definition: utils.h:1460

References v8::internal::GetCurrentStackPosition(), isolate_, js_stack_comparable_address_, v8::internal::SimulatorStack::RegisterCTryCatch(), v8::internal::Isolate::RegisterTryCatchHandler(), and ResetInternal().

+ Here is the call graph for this function:

◆ ~TryCatch()

v8::TryCatch::~TryCatch ( )

Unregisters and deletes this try/catch block.

Definition at line 1923 of file api.cc.

1923  {
1924  DCHECK(isolate_ == i::Isolate::Current());
1925  if (rethrow_) {
1926  v8::Isolate* isolate = reinterpret_cast<Isolate*>(isolate_);
1927  v8::HandleScope scope(isolate);
1929  if (HasCaught() && capture_message_) {
1930  // If an exception was caught and rethrow_ is indicated, the saved
1931  // message, script, and location need to be restored to Isolate TLS
1932  // for reuse. capture_message_ needs to be disabled so that DoThrow()
1933  // does not create a new message.
1934  isolate_->thread_local_top()->rethrowing_message_ = true;
1936  }
1939  reinterpret_cast<Isolate*>(isolate_)->ThrowException(exc);
1940  DCHECK(!isolate_->thread_local_top()->rethrowing_message_);
1941  } else {
1943  // If an exception was caught but is still scheduled because no API call
1944  // promoted it, then it is canceled to prevent it from being propagated.
1945  // Note that this will not cancel termination exceptions.
1947  }
1950  }
1951 }
A stack-allocated class that governs a number of local handles.
Definition: v8.h:802
Isolate represents an isolated instance of the V8 engine.
Definition: v8.h:4356
A light-weight stack-allocated object handle.
Definition: v8.h:334
static Local< T > New(Isolate *isolate, Handle< T > that)
Create a local handle for the content of another handle.
Definition: v8.h:5987
Local< Value > Exception() const
Returns the exception caught by this try/catch block.
Definition: api.cc:1976
bool HasCaught() const
Returns true if an exception has been caught by this try/catch block.
Definition: api.cc:1954
void RestorePendingMessageFromTryCatch(v8::TryCatch *handler)
Definition: isolate.cc:871
bool has_scheduled_exception()
Definition: isolate.h:627
void UnregisterTryCatchHandler(v8::TryCatch *that)
Definition: isolate.cc:249
void CancelScheduledExceptionFromTryCatch(v8::TryCatch *handler)
Definition: isolate.cc:887
ThreadLocalTop * thread_local_top()
Definition: isolate.h:878
static void UnregisterCTryCatch()
Definition: simulator-arm.h:55
#define DCHECK(condition)
Definition: logging.h:205

References DCHECK, v8::Exception, v8::Local< T >::New(), and v8::internal::SimulatorStack::UnregisterCTryCatch().

+ Here is the call graph for this function:

◆ TryCatch() [2/2]

v8::TryCatch::TryCatch ( const TryCatch )
private

Member Function Documentation

◆ CanContinue()

bool v8::TryCatch::CanContinue ( ) const

For certain types of exceptions, it makes no sense to continue execution.

If CanContinue returns false, the correct action is to perform any C++ cleanup needed and then return. If CanContinue returns false and HasTerminated returns true, it is possible to call CancelTerminateExecution in order to continue calling into the engine.

Definition at line 1959 of file api.cc.

1959  {
1960  return can_continue_;
1961 }

◆ Exception()

v8::Local< Value > v8::TryCatch::Exception ( ) const

Returns the exception caught by this try/catch block.

If no exception has been caught an empty handle is returned.

The returned handle is valid until this TryCatch block has been destroyed.

Definition at line 1976 of file api.cc.

1976  {
1977  DCHECK(isolate_ == i::Isolate::Current());
1978  if (HasCaught()) {
1979  // Check for out of memory exception.
1980  i::Object* exception = reinterpret_cast<i::Object*>(exception_);
1981  return v8::Utils::ToLocal(i::Handle<i::Object>(exception, isolate_));
1982  } else {
1983  return v8::Local<Value>();
1984  }
1985 }
void * exception_
Definition: v8.h:5402
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)

References DCHECK, and v8::Utils::ToLocal().

Referenced by v8::Shell::ReportException().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ HasCaught()

bool v8::TryCatch::HasCaught ( ) const

Returns true if an exception has been caught by this try/catch block.

Definition at line 1954 of file api.cc.

1954  {
1955  return !reinterpret_cast<i::Object*>(exception_)->IsTheHole();
1956 }

Referenced by v8::Shell::ExecuteString(), v8::HandleDebugEvent(), main(), and v8::Shell::Write().

+ Here is the caller graph for this function:

◆ HasTerminated()

bool v8::TryCatch::HasTerminated ( ) const

Returns true if an exception has been caught due to script execution being terminated.

There is no JavaScript representation of an execution termination exception. Such exceptions are thrown when the TerminateExecution methods are called to terminate a long-running script.

If such an exception has been thrown, HasTerminated will return true, indicating that it is possible to call CancelTerminateExecution in order to continue calling into the engine.

Definition at line 1964 of file api.cc.

1964  {
1965  return has_terminated_;
1966 }

◆ JSStackComparableAddress()

static void* v8::TryCatch::JSStackComparableAddress ( v8::TryCatch handler)
inlinestatic

There are cases when the raw address of C++ TryCatch object cannot be used for comparisons with addresses into the JS stack.

The cases are: 1) ARM, ARM64 and MIPS simulators which have separate JS stack. 2) Address sanitizer allocates local C++ object in the heap when UseAfterReturn mode is enabled. This method returns address that can be used for comparisons with addresses into the JS stack. When neither simulator nor ASAN's UseAfterReturn is enabled, then the address returned will be the address of the C++ try catch handler itself.

Definition at line 5386 of file v8.h.

5386  {
5387  if (handler == NULL) return NULL;
5388  return handler->js_stack_comparable_address_;
5389  }
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL

References js_stack_comparable_address_, and NULL.

Referenced by v8::internal::BASE_EMBEDDED< Visitor >::try_catch_handler_address().

+ Here is the caller graph for this function:

◆ Message()

v8::Local< v8::Message > v8::TryCatch::Message ( ) const

Returns the message associated with this exception.

If there is no message associated an empty handle is returned.

The returned handle is valid until this TryCatch block has been destroyed.

Definition at line 2012 of file api.cc.

2012  {
2013  DCHECK(isolate_ == i::Isolate::Current());
2014  i::Object* message = reinterpret_cast<i::Object*>(message_obj_);
2015  DCHECK(message->IsJSMessageObject() || message->IsTheHole());
2016  if (HasCaught() && !message->IsTheHole()) {
2018  } else {
2019  return v8::Local<v8::Message>();
2020  }
2021 }
void * message_obj_
Definition: v8.h:5403
static Local< Message > MessageToLocal(v8::internal::Handle< v8::internal::Object > obj)

References DCHECK, and v8::Utils::MessageToLocal().

Referenced by main(), and v8::Shell::ReportException().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator delete()

void v8::TryCatch::operator delete ( void *  ,
size_t   
)
private

◆ operator new()

void* v8::TryCatch::operator new ( size_t  size)
private

◆ operator=()

void v8::TryCatch::operator= ( const TryCatch )
private

◆ Reset()

void v8::TryCatch::Reset ( )

Clears any exceptions that may have been caught by this try/catch block.

After this method has been called, HasCaught() will return false. Cancels the scheduled exception if it is caught and ReThrow() is not called before.

It is not necessary to clear a try/catch block before using it again; if another exception is thrown the previously caught exception will just be overwritten. However, it is often a good idea since it makes it easier to determine which operation threw a given exception.

Definition at line 2024 of file api.cc.

2024  {
2025  DCHECK(isolate_ == i::Isolate::Current());
2027  // If an exception was caught but is still scheduled because no API call
2028  // promoted it, then it is canceled to prevent it from being propagated.
2029  // Note that this will not cancel termination exceptions.
2031  }
2032  ResetInternal();
2033 }

References DCHECK.

◆ ResetInternal()

void v8::TryCatch::ResetInternal ( )
private

Definition at line 2036 of file api.cc.

2036  {
2037  i::Object* the_hole = isolate_->heap()->the_hole_value();
2038  exception_ = the_hole;
2039  message_obj_ = the_hole;
2040  message_script_ = the_hole;
2041  message_start_pos_ = 0;
2042  message_end_pos_ = 0;
2043 }
void * message_script_
Definition: v8.h:5404
int message_end_pos_
Definition: v8.h:5407
int message_start_pos_
Definition: v8.h:5406

Referenced by TryCatch().

+ Here is the caller graph for this function:

◆ ReThrow()

v8::Handle< v8::Value > v8::TryCatch::ReThrow ( )

Throws the exception caught by this TryCatch in a way that avoids it being caught again by this same TryCatch.

As with ThrowException it is illegal to execute any JavaScript operations after calling ReThrow; the caller must return immediately to where the exception is caught.

Definition at line 1969 of file api.cc.

1969  {
1970  if (!HasCaught()) return v8::Local<v8::Value>();
1971  rethrow_ = true;
1972  return v8::Undefined(reinterpret_cast<v8::Isolate*>(isolate_));
1973 }
Handle< Primitive > Undefined(Isolate *isolate)
Definition: v8.h:6836

References v8::Undefined().

Referenced by v8::Shell::Write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetCaptureMessage()

void v8::TryCatch::SetCaptureMessage ( bool  value)

Set whether or not this TryCatch should capture a Message object which holds source information about where the exception occurred.

True by default.

Definition at line 2051 of file api.cc.

2051  {
2052  capture_message_ = value;
2053 }

◆ SetVerbose()

void v8::TryCatch::SetVerbose ( bool  value)

Set verbosity of the external exception handler.

By default, exceptions that are caught by an external exception handler are not reported. Call SetVerbose with true on an external exception handler to have exceptions caught by the handler reported as if they were not caught.

Definition at line 2046 of file api.cc.

2046  {
2047  is_verbose_ = value;
2048 }

Referenced by v8::Shell::ExecuteString(), and v8::internal::LiveEdit::GatherCompileInfo().

+ Here is the caller graph for this function:

◆ StackTrace()

v8::Local< Value > v8::TryCatch::StackTrace ( ) const

Returns the .stack property of the thrown object.

If no .stack property is present an empty handle is returned.

Definition at line 1988 of file api.cc.

1988  {
1989  DCHECK(isolate_ == i::Isolate::Current());
1990  if (HasCaught()) {
1991  i::Object* raw_obj = reinterpret_cast<i::Object*>(exception_);
1992  if (!raw_obj->IsJSObject()) return v8::Local<Value>();
1993  i::HandleScope scope(isolate_);
1994  i::Handle<i::JSObject> obj(i::JSObject::cast(raw_obj), isolate_);
1995  i::Handle<i::String> name = isolate_->factory()->stack_string();
1997  Maybe<bool> maybe = i::JSReceiver::HasProperty(obj, name);
1998  has_pending_exception = !maybe.has_value;
2000  if (!maybe.value) return v8::Local<Value>();
2001  i::Handle<i::Object> value;
2002  if (!i::Object::GetProperty(obj, name).ToHandle(&value)) {
2003  return v8::Local<Value>();
2004  }
2005  return v8::Utils::ToLocal(scope.CloseAndEscape(value));
2006  } else {
2007  return v8::Local<Value>();
2008  }
2009 }
#define EXCEPTION_BAILOUT_CHECK(isolate, value)
Definition: api.cc:93
#define EXCEPTION_PREAMBLE(isolate)
Definition: api.cc:67
Factory * factory()
Definition: isolate.h:982
static MUST_USE_RESULT Maybe< bool > HasProperty(Handle< JSReceiver > object, Handle< Name > name)
Definition: objects-inl.h:6646
static MUST_USE_RESULT MaybeHandle< Object > GetProperty(LookupIterator *it)
Definition: objects.cc:109
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name
Maybe< T > maybe(T t)
Definition: v8.h:902

References v8::internal::HandleScope::CloseAndEscape(), DCHECK, EXCEPTION_BAILOUT_CHECK, EXCEPTION_PREAMBLE, v8::internal::Object::GetProperty(), v8::internal::JSReceiver::HasProperty(), v8::maybe(), name, and v8::Utils::ToLocal().

Referenced by v8::Shell::ReportException().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ v8::internal::Isolate

friend class v8::internal::Isolate
friend

Definition at line 5414 of file v8.h.

Member Data Documentation

◆ can_continue_

bool v8::TryCatch::can_continue_
private

◆ capture_message_

bool v8::TryCatch::capture_message_
private

Definition at line 5410 of file v8.h.

Referenced by v8::internal::Isolate::DoThrow().

◆ exception_

void* v8::TryCatch::exception_
private

◆ has_terminated_

bool v8::TryCatch::has_terminated_
private

◆ is_verbose_

bool v8::TryCatch::is_verbose_
private

Definition at line 5408 of file v8.h.

Referenced by v8::internal::Isolate::ShouldReportException().

◆ isolate_

v8::internal::Isolate* v8::TryCatch::isolate_
private

Definition at line 5400 of file v8.h.

Referenced by TryCatch().

◆ js_stack_comparable_address_

void* v8::TryCatch::js_stack_comparable_address_
private

Definition at line 5405 of file v8.h.

Referenced by JSStackComparableAddress(), and TryCatch().

◆ message_end_pos_

int v8::TryCatch::message_end_pos_
private

Definition at line 5407 of file v8.h.

◆ message_obj_

void* v8::TryCatch::message_obj_
private

Definition at line 5403 of file v8.h.

◆ message_script_

void* v8::TryCatch::message_script_
private

Definition at line 5404 of file v8.h.

◆ message_start_pos_

int v8::TryCatch::message_start_pos_
private

Definition at line 5406 of file v8.h.

◆ next_

v8::TryCatch* v8::TryCatch::next_
private

Definition at line 5401 of file v8.h.

Referenced by v8::internal::Isolate::UnregisterTryCatchHandler().

◆ rethrow_

bool v8::TryCatch::rethrow_
private

Definition at line 5411 of file v8.h.


The documentation for this class was generated from the following files: