How to use JSException (attributes and methods)

Discussion in 'Java' started by Richard Maher, Jul 12, 2008.

  1. Hi,

    I've been doing a bit of Java Applet to JavaScript communication through
    JSObject.call() recently and am having much success. But when it comes to
    catching exceptions (in Java) that were thrown (in the JavaScript) I am not
    having much success at all :-(

    My try/catch(JSException e) block gets activated but the "e" seems to be
    pretty useless when it comes to interrogating what went wrong. The
    documentation says I should be able to get all sorts of JavaScript specific
    info like error-string, line-no, and file-name but all I get is null. Look,
    I'd settle for getMessage(), getWrappedThing().toString() or even
    printStackTrace().

    Can anyone please show me an example of an Applet trapping and deciphering a
    JS exception?

    Cheers Richard Maher

    PS. The exceptions can be anything you like from "object is null or
    undefined" to throw("A wobbly")
     
    Richard Maher, Jul 12, 2008
    #1
    1. Advertising

  2. Richard Maher

    Arne Vajhøj Guest

    Richard Maher wrote:
    > I've been doing a bit of Java Applet to JavaScript communication through
    > JSObject.call() recently and am having much success. But when it comes to
    > catching exceptions (in Java) that were thrown (in the JavaScript) I am not
    > having much success at all :-(
    >
    > My try/catch(JSException e) block gets activated but the "e" seems to be
    > pretty useless when it comes to interrogating what went wrong. The
    > documentation says I should be able to get all sorts of JavaScript specific
    > info like error-string, line-no, and file-name but all I get is null. Look,
    > I'd settle for getMessage(), getWrappedThing().toString() or even
    > printStackTrace().
    >
    > Can anyone please show me an example of an Applet trapping and deciphering a
    > JS exception?


    You should be able to do:

    try {
    // do some JSObject stuff
    } catch (JSException ex) {
    // newer Java
    Exception ex2 = (Exception)ex.getWrappedException();
    txtf.setText(ex2.getClass().getName() + " " +
    ex2.getMessage());
    } catch (Exception ex) {
    // prehistoric MS Java
    txtf.setText(ex.getClass().getName() + " " + ex.getMessage());
    }

    Arne
     
    Arne Vajhøj, Jul 13, 2008
    #2
    1. Advertising

  3. Hi Arne,

    Thanks for the reply.

    I'm getting null pointer exception from: -

    public void processMessage(String msgText)
    {
    int msgType = Integer.parseInt(msgText.substring(0,2));
    switch (msgType){
    case 1:
    chat.append(msgText.substring(2));
    break;
    case 2:
    String args[] = {msgText.substring(2)};
    try {browser.call("priceUpdate", args);}
    catch (JSException e)
    {
    System.out.println("Error when calling JS
    priceUpdate()");
    Exception ex2 =
    (Exception)e.getWrappedException();
    System.out.println(ex2.getClass().getName() +
    " " + ex2.getMessage());
    }
    break;
    default:
    System.out.println("Unknown rec type
    "+msgText);
    }
    }

    Have I misunderstood you?

    Cheers Richard Maher
    "Arne Vajhøj" <> wrote in message
    news:48797886$0$90265$...
    > Richard Maher wrote:
    > > I've been doing a bit of Java Applet to JavaScript communication through
    > > JSObject.call() recently and am having much success. But when it comes

    to
    > > catching exceptions (in Java) that were thrown (in the JavaScript) I am

    not
    > > having much success at all :-(
    > >
    > > My try/catch(JSException e) block gets activated but the "e" seems to be
    > > pretty useless when it comes to interrogating what went wrong. The
    > > documentation says I should be able to get all sorts of JavaScript

    specific
    > > info like error-string, line-no, and file-name but all I get is null.

    Look,
    > > I'd settle for getMessage(), getWrappedThing().toString() or even
    > > printStackTrace().
    > >
    > > Can anyone please show me an example of an Applet trapping and

    deciphering a
    > > JS exception?

    >
    > You should be able to do:
    >
    > try {
    > // do some JSObject stuff
    > } catch (JSException ex) {
    > // newer Java
    > Exception ex2 = (Exception)ex.getWrappedException();
    > txtf.setText(ex2.getClass().getName() + " " +
    > ex2.getMessage());
    > } catch (Exception ex) {
    > // prehistoric MS Java
    > txtf.setText(ex.getClass().getName() + " " +

    ex.getMessage());
    > }
    >
    > Arne
     
    Richard Maher, Jul 13, 2008
    #3
  4. Hi Again,

    Here's the JavaScript function: -

    function priceUpdate(netPrice){
    throw("A wobbly");
    var oldPrice = Number(currPrice.value);
    var newPrice = Number(netPrice);
    var movColor = "White";
    if (oldPrice < newPrice)
    movColor = "Turquoise";
    else
    if (oldPrice > newPrice)
    movColor = "Red";

    currPrice.value = newPrice;
    currPrice.style.backgroundColor = movColor;

    return;
    }

    Cheers Richard Maher
    "Richard Maher" <> wrote in message
    news:g5c635$qsi$...
    > Hi Arne,
    >
    > Thanks for the reply.
    >
    > I'm getting null pointer exception from: -
    >
    > public void processMessage(String msgText)
    > {
    > int msgType = Integer.parseInt(msgText.substring(0,2));
    > switch (msgType){
    > case 1:
    > chat.append(msgText.substring(2));
    > break;
    > case 2:
    > String args[] = {msgText.substring(2)};
    > try {browser.call("priceUpdate", args);}
    > catch (JSException e)
    > {
    > System.out.println("Error when calling JS
    > priceUpdate()");
    > Exception ex2 =
    > (Exception)e.getWrappedException();
    > System.out.println(ex2.getClass().getName()

    +
    > " " + ex2.getMessage());
    > }
    > break;
    > default:
    > System.out.println("Unknown rec type
    > "+msgText);
    > }
    > }
    >
    > Have I misunderstood you?
    >
    > Cheers Richard Maher
    > "Arne Vajhøj" <> wrote in message
    > news:48797886$0$90265$...
    > > Richard Maher wrote:
    > > > I've been doing a bit of Java Applet to JavaScript communication

    through
    > > > JSObject.call() recently and am having much success. But when it comes

    > to
    > > > catching exceptions (in Java) that were thrown (in the JavaScript) I

    am
    > not
    > > > having much success at all :-(
    > > >
    > > > My try/catch(JSException e) block gets activated but the "e" seems to

    be
    > > > pretty useless when it comes to interrogating what went wrong. The
    > > > documentation says I should be able to get all sorts of JavaScript

    > specific
    > > > info like error-string, line-no, and file-name but all I get is null.

    > Look,
    > > > I'd settle for getMessage(), getWrappedThing().toString() or even
    > > > printStackTrace().
    > > >
    > > > Can anyone please show me an example of an Applet trapping and

    > deciphering a
    > > > JS exception?

    > >
    > > You should be able to do:
    > >
    > > try {
    > > // do some JSObject stuff
    > > } catch (JSException ex) {
    > > // newer Java
    > > Exception ex2 = (Exception)ex.getWrappedException();
    > > txtf.setText(ex2.getClass().getName() + " " +
    > > ex2.getMessage());
    > > } catch (Exception ex) {
    > > // prehistoric MS Java
    > > txtf.setText(ex.getClass().getName() + " " +

    > ex.getMessage());
    > > }
    > >
    > > Arne

    >
    >
     
    Richard Maher, Jul 13, 2008
    #4
  5. Richard Maher

    Arne Vajhøj Guest

    Richard Maher wrote:
    > I'm getting null pointer exception from: -
    >
    > public void processMessage(String msgText)
    > {
    > int msgType = Integer.parseInt(msgText.substring(0,2));
    > switch (msgType){
    > case 1:
    > chat.append(msgText.substring(2));
    > break;
    > case 2:
    > String args[] = {msgText.substring(2)};
    > try {browser.call("priceUpdate", args);}
    > catch (JSException e)
    > {
    > System.out.println("Error when calling JS priceUpdate()");
    > Exception ex2 = (Exception)e.getWrappedException();
    > System.out.println(ex2.getClass().getName() + " " + ex2.getMessage());
    > }
    > break;
    > default:
    > System.out.println("Unknown rec type "+msgText);
    > }
    > }
    >
    > Have I misunderstood you?


    No.

    But maybe you do not get a wrapped exception for some reason.

    Maybe:

    Exception ex2 = (Exception)e.getWrappedException();
    if(ex2 != null) {
    System.out.println(ex2.getClass().getName() + " " + ex2.getMessage());
    } else {
    System.out.println(ex.getClass().getName() + " " + ex.getMessage());
    }

    Arne
     
    Arne Vajhøj, Jul 20, 2008
    #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. Max
    Replies:
    1
    Views:
    484
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,826
    bruce barker
    Jan 31, 2007
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    377
    Bruno Desthuilliers
    Jun 20, 2007
  4. Kyle Schmitt
    Replies:
    3
    Views:
    194
    Kyle Schmitt
    Jul 24, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    324
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page