Path: csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news-1.dfn.de!news.dfn.de!cache.uni-koblenz.de!not-for-mail From: Tassilo Horn Newsgroups: comp.lang.java.programmer Subject: Re: What to do with an exception of Field.get() Date: Fri, 28 Aug 2015 16:52:37 +0200 Organization: University Koblenz-Landau Campus Koblenz Lines: 35 Message-ID: <878u8v8nyi.fsf@gnu.org> References: <87fv33fsa6.fsf@Equus.decebal.nl> <87bndrfq7c.fsf@Equus.decebal.nl> NNTP-Posting-Host: 2.160.110.50 Mime-Version: 1.0 Content-Type: text/plain X-Trace: cache.uni-koblenz.de 1440773558 24656 2.160.110.50 (28 Aug 2015 14:52:38 GMT) X-Complaints-To: news@cache.uni-koblenz.de NNTP-Posting-Date: Fri, 28 Aug 2015 14:52:38 +0000 (UTC) User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) Cancel-Lock: sha1:4msJv/8gVXt/5l0HUvSnuUlhG/k= Xref: csiph.com comp.lang.java.programmer:32437 Cecil Westerhof writes: >> 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). You can get a stack trace of every exception. > 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 wouldn't be different if you didn't handle the exception at all and just declared to throw the same exceptions as Field.get(). Then the user of your method could either have one `catch (Exception e)' clause or multiple clauses catching the exceptions which they can handle. Well, in case your custom exception class derives from RuntimeException the user is free not to handle the exceptions at all whereas he would need to handle at least the IllegalAccessException otherwise. I wouldn't argue that this is a good thing... Bye, Tassilo