[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

try ... catch for a specific error ?

R.Wieser

6/2/2016 7:54:00 AM

Hello all,

I'm executing a command which can return a specific error constant
indicating the item is not found. I would like to catch that error alone
(returning an empty string instead of failing), and have the system handle
all others.

Two seperate questions:
1) Can I tell the "catch" to only do so on (a) specific error(s) ?

if not than:

2) how can I ...

a) compare the error constant against the "err" object (it does not seem to
expose it)

b) tell the system to handle/continue with the errors I have not handled
myself.

Regards,
Rudy Wieser



20 Answers

Evertjan.

6/2/2016 9:21:00 AM

0

"R.Wieser" <address@not.available> wrote on 02 Jun 2016 in
comp.lang.javascript:

> Hello all,
>
> I'm executing a command which can return a specific error constant
> indicating the item is not found. I would like to catch that error alone
> (returning an empty string instead of failing), and have the system handle
> all others.
>
> Two seperate questions:
> 1) Can I tell the "catch" to only do so on (a) specific error(s) ?
>
> if not than:
>
> 2) how can I ...
>
> a) compare the error constant against the "err" object (it does not seem
to
> expose it)
>
> b) tell the system to handle/continue with the errors I have not handled
> myself.

Try..catch seems to me for "inspecific" errors,
not for an "executed command" [what is that, btw?]
returning a specific value hat you interpret as an error-vallue.

Why not just:

var myErrorConstant = myExecutedCommand();

if (myErrorConstant != 0)
console.log('myErrorConstant=' + myErrorConstant);

or

var myErrorConstant = myExecutedCommand();

if (myErrorConstant != 0)
alert('myErrorConstant=' + myErrorConstant);



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

R.Wieser

6/2/2016 10:19:00 AM

0

Evertjan,

> "executed command" [what is that, btw?]

An attempt of a generic description for calling something that returns data.
In this specific case its calling method of an object (but could be as
easily be a function).

> Why not just:
>
> var myErrorConstant = myExecutedCommand();

Thats because the "myExecutedCommand()" is not mine (have no control over)
and does not return an error, but throws it (hence the need for a "try ...
catch" wrapping).

Regards,
Rudy Wieser


-- Origional message
Evertjan. <exxjxw.hannivoort@inter.nl.net> schreef in berichtnieuws
XnsA61B73815550eejj99@194.109.6.166...
>
> Try..catch seems to me for "inspecific" errors,
> not for an "executed command" [what is that, btw?]
> returning a specific value hat you interpret as an error-vallue.
>
> Why not just:
>
> var myErrorConstant = myExecutedCommand();
>
> if (myErrorConstant != 0)
> console.log('myErrorConstant=' + myErrorConstant);
>
> or
>
> var myErrorConstant = myExecutedCommand();
>
> if (myErrorConstant != 0)
> alert('myErrorConstant=' + myErrorConstant);
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)



Christoph M. Becker

6/2/2016 10:38:00 AM

0

On 02.06.2016 at 09:54, R.Wieser wrote:

> I'm executing a command which can return a specific error constant
> indicating the item is not found. I would like to catch that error alone
> (returning an empty string instead of failing), and have the system handle
> all others.
>
> Two seperate questions:
> 1) Can I tell the "catch" to only do so on (a) specific error(s) ?

You can do something like:

try {
// ...
} catch (ex) {
if (weAreInterestedInHandlingTheException(ex)) {
// handle the exception
} else {
throw ex;
}
}

--
Christoph M. Becker

Evertjan.

6/2/2016 11:23:00 AM

0

"R.Wieser" <address@not.available> wrote on 02 Jun 2016 in
comp.lang.javascript:

>> Why not just:
>>
>> var myErrorConstant = myExecutedCommand();
>
> Thats because the "myExecutedCommand()" is not mine (have no control over)
> and does not return an error, but throws it (hence the need for a "try ...
> catch" wrapping).

Okay.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

R.Wieser

6/2/2016 12:33:00 PM

0

Christoph,

> You can do something like:
....
> throw ex;

Thanks, I was not aware that I could re-throw the origional error like that.

But that leaves my a) question: how do I compare the, in my case being the
NS_ERROR_NOT_AVAILABLE error-constant, to the returned "ex" object ?
Documentation as provided by
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...
ects/Error does not mention such a property ...

Regards,
Rudy Wieser


-- Origional message
Christoph M. Becker <cmbecker69@arcor.de> schreef in berichtnieuws
nip2aa$lvc$1@solani.org...
>
> You can do something like:
>
> try {
> // ...
> } catch (ex) {
> if (weAreInterestedInHandlingTheException(ex)) {
> // handle the exception
> } else {
> throw ex;
> }
> }
>
> --
> Christoph M. Becker


Christoph M. Becker

6/2/2016 12:40:00 PM

0

On 02.06.2016 at 14:32, R.Wieser wrote:

>> You can do something like:
> ...
>> throw ex;
>
> Thanks, I was not aware that I could re-throw the origional error like that.
>
> But that leaves my a) question: how do I compare the, in my case being the
> NS_ERROR_NOT_AVAILABLE error-constant, to the returned "ex" object ?
> Documentation as provided by
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...
> ects/Error does not mention such a property ...

Most likely for a good reason: this error constant appears to be highly
user-agent specific. I would try my best to avoid messing with such a
constant, and indeed concentrate on the root cause: why is something
(what?) not available in the first place?

--
Christoph M. Becker

JJ

6/2/2016 1:24:00 PM

0

On Thu, 2 Jun 2016 14:32:47 +0200, R.Wieser wrote:
> Christoph,
>
>> You can do something like:
> ....
>> throw ex;
>
> Thanks, I was not aware that I could re-throw the origional error like that.
>
> But that leaves my a) question: how do I compare the, in my case being the
> NS_ERROR_NOT_AVAILABLE error-constant, to the returned "ex" object ?
> Documentation as provided by
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...
> ects/Error does not mention such a property ...

That is Firefox's XPCOM generated exception. Its object specifications is
not part of any web standards.

If you really need to check that, read the exception object's "name"
property. It's a string type value. e.g.

try {
//...
} catch(e) {
if ("function" === typeof e.QueryInterface) { //if XPCOM exception...
if (e.name === "NS_ERROR_NOT_AVAILABLE") {
console.log("XPCOM Error: Not available");
}
}
}

R.Wieser

6/2/2016 1:40:00 PM

0

Christoph,

> Most likely for a good reason: this error constant appears to be
> highly user-agent specific.

Its not about that specific constant (regard it as an example), but about
how I compare *any* error-constant to the returned error/exception object.
Can I check if a specific error has been thrown, and if so how ?

As for that constant being rather user-agent specific ? JavaScript isn't
only used in webpages. :-)

Regards,
Rudy Wieser


-- Origional message:
Christoph M. Becker <cmbecker69@arcor.de> schreef in berichtnieuws
nip9et$h5d$1@solani.org...
>
> Most likely for a good reason: this error constant appears to be highly
> user-agent specific. I would try my best to avoid messing with such a
> constant, and indeed concentrate on the root cause: why is something
> (what?) not available in the first place?
>
> --
> Christoph M. Becker
>


Christoph M. Becker

6/2/2016 2:03:00 PM

0

On 02.06.2016 at 15:40, R.Wieser wrote:

>> Most likely for a good reason: this error constant appears to be
>> highly user-agent specific.
>
> Its not about that specific constant (regard it as an example), but about
> how I compare *any* error-constant to the returned error/exception object.
> Can I check if a specific error has been thrown, and if so how ?

You can apply the usual comparison operators, typeof, instanceof etc.
Note that JavaScript allows objects and scalar values to be thrown.

For your concrete case see JJ's response
(<news:yua8gzzjdwv9$.z2ar9grudtz1$.dlg@40tude.net>).

> As for that constant being rather user-agent specific ? JavaScript isn't
> only used in webpages. :-)

Indeed, I should have written "host environment specific".

--
Christoph M. Becker

R.Wieser

6/2/2016 2:24:00 PM

0

Christoph,

> Indeed, I should have written "host environment specific".

.... and even that its doesn't matter in the slightest I'm afraid.

And it would also be even less usefull as a designation, as "host" can be
used for both an OS, a browser and, with a little bit of will, even for a
webpage ...

> > Can I check if a specific error has been thrown, and if so how ?
>
> You can apply the usual comparison operators, typeof, instanceof etc.

I'm sorry, but thats a rather unspecific, and as such unusable reply. :-(

Thanks for trying though.

Regards,
Rudy Wieser


-- Origional message:
Christoph M. Becker <cmbecker69@arcor.de> schreef in berichtnieuws
nipeap$4db$1@solani.org...
....
> > Can I check if a specific error has been thrown, and if so how ?
>
> You can apply the usual comparison operators, typeof, instanceof etc.
> Note that JavaScript allows objects and scalar values to be thrown.
>
> For your concrete case see JJ's response
> (<news:yua8gzzjdwv9$.z2ar9grudtz1$.dlg@40tude.net>).
>
> > As for that constant being rather user-agent specific ? JavaScript
isn't
> > only used in webpages. :-)
>
> Indeed, I should have written "host environment specific".
>
> --
> Christoph M. Becker