Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: What to do with an exception of Field.get() Date: Fri, 28 Aug 2015 10:44:53 -0400 Organization: A noiseless patient Spider Lines: 44 Message-ID: References: <87fv33fsa6.fsf@Equus.decebal.nl> <87bndrfq7c.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 28 Aug 2015 14:43:11 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="4388"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pNp5kZalo3IjoZ+T1cZBO" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: <87bndrfq7c.fsf@Equus.decebal.nl> Cancel-Lock: sha1:yvyXzlVusvrN4WGW5xIUJxAdodM= Xref: csiph.com comp.lang.java.programmer:32436 On 8/28/2015 10:22 AM, Cecil Westerhof wrote: > On Friday 28 Aug 2015 15:37 CEST, Cecil Westerhof wrote: > >> I am using: >> http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html#get%28java.lang.Object%29 >> >> This can throw an IllegalArgumentException, or an >> IllegalAccessException. >> >> I see this as an unacceptable error and I am thinking about giving a >> stacktrace and exiting the program. >> >> Is this acceptable, or is it not done? > > Answering my own question. ;-) > > In principle you never know where your function is used. (If you share > it.) So I wrote a custom exception (which gives a stacktrace). In this > way the user only has to check for one exception (it is used in Java > 6) and can decide what to do. That seems the right approach. If you haven't done so already, it's a good idea to propagate the original exception as the "cause" of the one eventually thrown: try { ... } catch (IllegalArgumentException | IllegalAccessException ex) { throw new WrapperException("Yikes!", ex); } or (if the wrapping exception lacks a suitable constructor): try { ... } catch (IllegalArgumentException | IllegalAccessException ex) { Exception wrapper = new WrapperException("Yikes!"); wrapper.initCause(ex); throw wrapper; } -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM