Path: csiph.com!news.bbs.geek.nz!.POSTED.agency.bbs.geek.nz!not-for-mail From: Eric Sosman (Eric Sosman) Newsgroups: comp.lang.java.programmer Subject: Re: Two exceptions Date: Sat, 09 Dec 2017 14:55:38 +1200 Organization: fsxNet Usenet Gateway | bbs.geek.nz/#fsxNet Message-ID: <168218953@f38.n261.z1.binkp.net> References: <3577663232@f38.n261.z1.binkp.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: news.bbs.geek.nz; posting-host="M6YmRdZYyc42DJk0lNlt/X4dpP4dzvceBNabSmESN3E"; logging-data="1216"; mail-complaints-to="abuse@news.bbs.geek.nz" User-Agent: VSoup v1.2.9.47Beta [95/NT] X-Comment-To: Stefan Ram X-MailConverter: SoupGate-Win32 v1.05 Xref: csiph.com comp.lang.java.programmer:37793 On 12/9/2017 11:15 AM, Stefan Ram wrote: > Victor Porton writes: >> So I have two alternatives: >> 1. Derive ParseException from FatalParseException what seems to do the right >> thing but seems illogical. > > The type of a result of a parse is > > class ParseResult { ... > > which can have subclasses > > class ProperParseResult extends ParseResult { ... > class ErrorParseResult extends ParseResult { ... > > which in turn can have several subclasses each of which > might implement several interfaces. > > A parseResult might contain other parseResults (AST). This would work, yes, but I think it would lead to clumsy code at the call site, something like ParseResult result = parse(); if (result instanceof ProperParseResult) { ProperParseResult ppr = (ProperParseResult) result; ... } else if (result instanceof ErrorParseResult) { ErrorParseResult epr = (ErrorParseResult) result; ... } else { throw new IllegalStateException("bad result", result); } That is, you'd need to use `instanceof' (or equivalent) to learn what kind of a result you obtained -- and if you find yourself using `instanceof' a lot, that's usually a sign that the design could be improved. (It can be argued that `try/catch' is a disguised `instanceof', particularly if there are multiple `catch' clauses. This seems forgivable, though, on the grounds that exceptions are "unusual.") -- esosman@comcast-dot-net.invalid Eleven hundred thirty-eight days to go.