throw without try..catch

F

Fred

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?
 
C

crater

Fred said:
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
 
F

Fred

crater said:
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.
 
R

Richard Cornford

Fred said:
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.
 
F

Fred

Richard said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top