How to use JSException (attributes and methods)

R

Richard Maher

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")
 
A

Arne Vajhøj

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

Richard Maher

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
 
R

Richard Maher

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 said:
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
am
not

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
 
A

Arne Vajhøj

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

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top