throw without try..catch

Discussion in 'Javascript' started by Fred, Dec 14, 2006.

  1. Fred

    Fred Guest

    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
    Fred, Dec 14, 2006
    #1
    1. Advertising

  2. Fred

    crater Guest

    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
    crater, Dec 14, 2006
    #2
    1. Advertising

  3. Fred

    Fred Guest

    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
    Fred, Dec 14, 2006
    #3
  4. 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.
    Richard Cornford, Dec 14, 2006
    #4
  5. Fred

    Fred Guest

    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
    Fred, Dec 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    7
    Views:
    4,260
    Jon Maz
    Oct 25, 2004
  2. Matt
    Replies:
    1
    Views:
    399
    Michael Rauscher
    Jun 12, 2004
  3. Chris Riesbeck

    try-catch-throw-finally

    Chris Riesbeck, Jan 19, 2007, in forum: Java
    Replies:
    3
    Views:
    1,021
    Chris Riesbeck
    Jan 22, 2007
  4. Replies:
    8
    Views:
    344
    Matteo
    Dec 5, 2005
  5. dick

    "try{throw}catch"

    dick, Nov 15, 2006, in forum: C++
    Replies:
    7
    Views:
    400
    Noah Roberts
    Nov 15, 2006
Loading...

Share This Page