Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > throw without try..catch

Reply
Thread Tools

throw without try..catch

 
 
Fred
Guest
Posts: n/a
 
      12-14-2006
Is it possible to use throw in javascript without try..catch? As far
as I know, you must call it from within a try..catch block, or the
function that calls throw must itself be called from within try..catch,
e.g.:


function xTest(x) {
if (!x) throw 'x failed';
return 'x passed';
}

function doXTest(x){
try {
alert( xTest(x) );
} catch (e){
alert('xTest threw an error: ' + e)
}
}

doXTest(false); // -> 'xTest threw an error: x failed'
doXTest(true); // -> 'x passed'


that is, xTest() must be called from within a try..catch block because
if the test fails, throw will not be caught. Or am I missing
something?


--
Fred

 
Reply With Quote
 
 
 
 
crater
Guest
Posts: n/a
 
      12-14-2006

Fred wrote:
> Is it possible to use throw in javascript without try..catch? As far
> as I know, you must call it from within a try..catch block, or the
> function that calls throw must itself be called from within try..catch,
> e.g.:
>


You can throw exceptions from just about anywhere. Look at this example
from the docs...

function UserException (message) {
this.message=message;
this.name="UserException";
}
function getMonthName (mo) {
mo=mo-1; // Adjust month number for array index (1=Jan, 12=Dec)
var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec");
if (months[mo] != null) {
return months[mo];
} else {
myUserException=new UserException("InvalidMonthNo");
throw myUserException;
}
}

............
try {
// statements to try;
monthName=getMonthName(myMonth)
}
catch (e) {
monthName="unknown";
logMyErrors(e.message,e.name); // pass exception object to err
handler
}

Regards, crater

 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      12-14-2006
crater wrote:
> Fred wrote:
> > Is it possible to use throw in javascript without try..catch? As far
> > as I know, you must call it from within a try..catch block, or the
> > function that calls throw must itself be called from within try..catch,
> > e.g.:
> >

>
> You can throw exceptions from just about anywhere.
> Look at this example from the docs...


I did. It is an example of precisely what I posted - a function using
throw that is called from within a try..catch block.

What I want to know is if you can use throw *without* a try..catch
block. I'm pretty sure you can't - if the throw branch is taken and the
function wasn't called from within a try..catch, I always get an error.
I just want to know whether I've correctly understood its conditions
for use or not.


--
Fred

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      12-14-2006
Fred wrote:
> crater wrote:
> > Fred wrote:
> > > Is it possible to use throw in javascript without try..catch? As far
> > > as I know, you must call it from within a try..catch block, or the
> > > function that calls throw must itself be called from within try..catch,
> > > e.g.:
> > >

> >
> > You can throw exceptions from just about anywhere.
> > Look at this example from the docs...

>
> I did. It is an example of precisely what I posted - a function
> using throw that is called from within a try..catch block.
>
> What I want to know is if you can use throw *without* a
> try..catch block. I'm pretty sure you can't -


You can - throw - from anywhere that you can use an expression.

> if the throw branch is taken and the function wasn't called
> from within a try..catch, I always get an error.


A runtime error is an uncaught exception. If you throw an exception
yourself and it is never caught it will propagate up and eventually
appear in whatever error reporting mechanism the javascript engine has
(and terminate the execution of the script.

> I just want to know whether I've correctly understood its
> conditions for use or not.


Apparently you have not. However, try-catch is only rarely useful in
javascript as it is better to avoid errors than attempt to handle them.

Richard.

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      12-14-2006
Richard Cornford wrote:
> Fred wrote:

[...]
> > What I want to know is if you can use throw *without* a
> > try..catch block. I'm pretty sure you can't -

>
> You can - throw - from anywhere that you can use an expression.
>
> > if the throw branch is taken and the function wasn't called
> > from within a try..catch, I always get an error.

>
> A runtime error is an uncaught exception. If you throw an exception
> yourself and it is never caught it will propagate up and eventually
> appear in whatever error reporting mechanism the javascript engine has
> (and terminate the execution of the script.


Thanks. I think the old version of Firebug reported "error thrown
without catch" or similar, the new version reports "uncaught exception"
(which is consistent with the error message generated by the browsers I
tested).

I think I understand it now.


> However, try-catch is only rarely useful in
> javascript as it is better to avoid errors than attempt to handle them.


Sure, its use is to be avoided - I don't use it in my own code - but it
is part of the language and is used in a few well known libraries
evangelised by some and I wanted to better understand it.


--
Fred

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
clocking muxing, plz throw some light sudeepts@gmail.com VHDL 1 02-23-2006 06:43 PM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
throw an application wide event in an asp.net page PrashanthNagaraj ASP .Net 2 11-24-2003 12:48 AM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM



Advertisments