473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

throw without try..catch

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

Dec 14 '06 #1
4 8149

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

Dec 14 '06 #2
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

Dec 14 '06 #3
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.

Dec 14 '06 #4
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

Dec 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cody | last post by:
I have a method that gets called if the user presses a certain button. If the object is in a state that doesn't allow the calling of that method, what should I do? Should I better throw an...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
24
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass();...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
8
by: Grizlyk | last post by:
Hello I want to understand the exception habdling in C++ more, because i think no one can apply anything free (free - without remembering large unclear list of rules and exceptions from rules...
6
by: Fir5tSight | last post by:
Hi, What does "throw" do? I understand that it throws an error when certain exceptional situation happens. My guess is that this *ignores* the error and *continues* so that it prevents the...
7
by: AshokG | last post by:
Hi, If you use just throw without parameter should preserve the complete stack trace and the exception information. for example: 1. private void Bar() 2. { 3. try
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.