Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #38393
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Exception handling (was: check for obj nested prop) |
| Date | 2020-05-09 03:46 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <5599059.lOV4Wx5bFT@PointedEars.de> (permalink) |
| References | <C9udnTlJHfCQbSnDnZ2dnUU7-U3NnZ2d@westnet.com.au> <87pnbeycrm.fsf@bsb.me.uk> |
Ben Bacarisse wrote:
> Andrew Poulos <ap_prog@hotmail.com> writes:
>> let obj = {a: 1};
>> if (a.b.c.d) // throws error
>
> Did you mean if (obj.a.b.c.d) ?
Probably yes.
>> so I end up doing
>>
>> if (a && a.b && a.b.c && a.b.c.d)
> <cut>
>> Is there a way to get the check value of an object's nested property
>> without first having to test for all the ascendant properties?
>
> if (try {obj.a.b.c.d} catch {})
>
> might do what you want.
By contrast, the following is syntactically correct:
try {
if (obj.a.b.c.d)
{
// …
}
} catch {}
However, there are several caveats:
1. The omission of the exception parameter requires an implementation of
ECMAScript Edition 10 (ECMAScript 2019). This is the most recent
Edition of the standard and requires the most recent version of a runtime
environment (e.g. a Web browser) to have a chance to be implemented.
Compare
<http://ecma-international.org/ecma-262/9.0/#prod-Catch>
with
<http://ecma-international.org/ecma-262/10.0/#prod-Catch>
2. Using an empty try-catch block can hide problems in code that are not
syntax errors, and is strongly recommended against (as in “SHOULD NOT”).
For example, the code above hides a problem of an undeclared identifier
“obj” even in strict mode, which would throw a ReferenceError exception.
Therefore, if try-catch is used, in almost all cases exception handling
should be specific.
Unfortunately, ECMAScript did not standardize a feature of Mozilla
JavaScript where it was possible to write
catch (e if e instanceof ExceptionType) {
// …
}
where ExceptionType would be the exception type, e.g. TypeError,
and as of Mozilla JavaScript 68 (Firefox 68) it is removed from
the implementation.
However,
try {
}
catch (e) {
if (e instanceof ExceptionType) {
// …
}
}
is functionally equivalent and strictly standards-compliant
(specified explicitly in the standard).
Finally, exceptions that are not handled should be re-thrown so as to
not hide more serious problems:
catch (e) {
if (e instanceof ExceptionType) {
// …
}
else if (e instanceof …) {
// …
}
else throw e;
}
3. With that said, it is actually not easy to tell exception conditions
apart. For example, if a property-read access results in the “undefined”
value because an object does not have a property with the specified name,
then the subsequent lookup of a “nested” property will throw a TypeError
exception because “undefined has no properties” (it is a primitive
value that cannot be converted to an object).
But a TypeError exception is being thrown by several other operations as
well (see the standard’s prose), so the recommended *alternative* code
try {
if (obj.a.b.c.d)
{
// …
}
} catch (e) {
if (e instanceof TypeError) {
// …
}
else throw e;
}
is *unable* to tell those conditions apart, and there is none more
specific exception type, e.g. a “KeyError“ exception (as it is available
for example in Python).
Surprising to me, the Airbnb JavaScript Style Guide¹ makes no explicit
recommendation or requirement for exception handling. However, they refer
to a jsPerf test case for “Try/Catch Cost In a Loop”:
<https://jsperf.com/try-catch-in-loop-cost/12>
Also, you should be aware that while ESLint² supports defining several rules
for “throw” and “catch”³, none of them can protect against the problems with
exception handling that I discussed above.
______
¹ <https://github.com/airbnb/javascript>
² <https://eslint.org/>
³ <https://eslint.org/docs/rules/>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
check for obj nested prop Andrew Poulos <ap_prog@hotmail.com> - 2020-05-08 15:37 +1000
Re: check for obj nested prop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-05-08 10:52 +0100
Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-09 00:26 +0200
Re: check for obj nested prop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-05-09 03:09 +0100
Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-12 09:28 +0200
Re: check for obj nested prop Julio Di Egidio <julio@diegidio.name> - 2020-05-12 04:01 -0700
Exception handling (was: check for obj nested prop) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-09 03:46 +0200
Re: check for obj nested prop JJ <jj4public@vfemail.net> - 2020-05-08 17:18 +0700
Re: check for obj nested prop Julio Di Egidio <julio@diegidio.name> - 2020-05-08 03:53 -0700
Re: check for obj nested prop James Kirk <noneya.invalid@gmail.com> - 2020-05-08 11:00 -0400
Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-09 00:19 +0200
Re: check for obj nested prop Julio Di Egidio <julio@diegidio.name> - 2020-05-12 04:04 -0700
Re: check for obj nested prop luserdroog <luser.droog@gmail.com> - 2020-05-12 07:59 -0700
Re: check for obj nested prop "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-05-12 14:17 -0700
Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-13 02:46 +0200
Re: check for obj nested prop Andrew Poulos <ap_prog@hotmail.com> - 2020-05-13 13:08 +1000
Re: check for obj nested prop "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-05-13 12:30 -0700
csiph-web