How to use TryCatch properly?

71 views
Skip to first unread message

Stephan Beal

unread,
Jul 20, 2011, 10:27:13 PM7/20/11
to v8-users
Hello, v8 gurus,

       v8::TryCatch tryCatch;
        tryCatch.SetVerbose(true);
        tryCatch.SetCaptureMessage(true);
        v8::ThrowException(v8::String::New("CATCH THIS, DAMMIT!"));
        assert( tryCatch.HasCaught() );
        assert( !tryCatch.Exception().IsEmpty() );

i can't for the life of me figure out how to use TryCatch properly. Both of those assertions fail (well, whichever one is enabled fails).

What am i missing here?

:-?

Mads Sig Ager

unread,
Jul 21, 2011, 3:08:11 AM7/21/11
to v8-u...@googlegroups.com
v8::ThrowException schedules an exception to be thrown when returning to JavaScript. At the point where you ask if it has been caught it has not been thrown yet.

Cheers,    -- Mads

Stephan Beal

unread,
Jul 21, 2011, 3:11:31 AM7/21/11
to v8-u...@googlegroups.com
On Thu, Jul 21, 2011 at 9:08 AM, Mads Sig Ager <ag...@chromium.org> wrote:
v8::ThrowException schedules an exception to be thrown when returning to JavaScript. At the point where you ask if it has been caught it has not been thrown yet.

i see. Thank you for that clarification.

The return value of ThrowException(): is it Undefined() or an Empty handle? i recall (perhaps incorrectly) reading both descriptions over the years.

Mads Sig Ager

unread,
Jul 21, 2011, 4:16:43 AM7/21/11
to v8-u...@googlegroups.com
On Thu, Jul 21, 2011 at 9:11 AM, Stephan Beal <sgb...@googlemail.com> wrote:
On Thu, Jul 21, 2011 at 9:08 AM, Mads Sig Ager <ag...@chromium.org> wrote:
v8::ThrowException schedules an exception to be thrown when returning to JavaScript. At the point where you ask if it has been caught it has not been thrown yet.

i see. Thank you for that clarification.

The return value of ThrowException(): is it Undefined() or an Empty handle? i recall (perhaps incorrectly) reading both descriptions over the years.

It is undefined. 

It *can* return an empty handle, but only if calling into V8 before it is initialized or after it is dead (after an out of memory or similar). 

Cheers,    -- Mads

 
--

Stephan Beal

unread,
Jul 21, 2011, 4:21:06 AM7/21/11
to v8-u...@googlegroups.com
On Thu, Jul 21, 2011 at 9:08 AM, Mads Sig Ager <ag...@chromium.org> wrote:
v8::ThrowException schedules an exception to be thrown when returning to JavaScript. At the point where you ask if it has been caught it has not been thrown yet.

A slight clarification: does "returning to JS" mean returning to a higher scope than the TryCatch, or calling back into v8 (perhaps in a deeper scope than the TryCatch, where the TC is still alive)?

Mads Sig Ager

unread,
Jul 21, 2011, 4:36:41 AM7/21/11
to v8-u...@googlegroups.com
On Thu, Jul 21, 2011 at 10:21 AM, Stephan Beal <sgb...@googlemail.com> wrote:
On Thu, Jul 21, 2011 at 9:08 AM, Mads Sig Ager <ag...@chromium.org> wrote:
v8::ThrowException schedules an exception to be thrown when returning to JavaScript. At the point where you ask if it has been caught it has not been thrown yet.

A slight clarification: does "returning to JS" mean returning to a higher scope than the TryCatch, or calling back into v8 (perhaps in a deeper scope than the TryCatch, where the TC is still alive)?

It means actually returning to a JavaScript activation on the stack. Returning to other C++ code will not throw the exception.

-- Mads 
 
--

Stephan Beal

unread,
Jul 21, 2011, 4:38:26 AM7/21/11
to v8-u...@googlegroups.com
On Thu, Jul 21, 2011 at 10:36 AM, Mads Sig Ager <ag...@chromium.org> wrote:
It means actually returning to a JavaScript activation on the stack. Returning to other C++ code will not throw the exception.

Okay, that explains my code's broken behaviour. Thank you :).
 
Reply all
Reply to author
Forward
0 new messages