Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32899

Re: Way to avoid repeating to catch the same exception in overridden method

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Way to avoid repeating to catch the same exception in overridden method
Date 2015-10-19 12:11 -0400
Organization A noiseless patient Spider
Message-ID <n02mgp$g5i$1@dont-email.me> (permalink)
References <fd943245-c84e-4b93-9ed5-e18fb25e256a@googlegroups.com>

Show all headers | View raw


On 10/19/2015 4:02 AM, mike wrote:
> Hi,
>
>
> We have the current implementation in our overridden toString() method:
>
> public String toString() {
>          return "firstName=" + res.getFirstName() + "{" +
>                  "lastName=" + res.getLastName() + ", " +
>                  "address=" + res.getAddress() + "/" + "***" + ", " +
>                  "zipCode=" + resource.getZipCode() + "}";
>      }
>
>
>
> Each get can throw an IllegalArgumentExcepion so we need to replace the value with an empty String if it happens( to avoid that our user in this case should see an IllegalArgumentException ).
>
> My question is if there is a more efficient way to override toString() than:
>
> String firstName;
> String lastName;
> try {
> firstName = res.getFirstName();
>
> }catch(IllegalArgumentException e){
> firstName = "";
> }
>
> try{
>
> lastName = res.getLastName();
> }catch(IllegalArgumentException e2){
> lastName = "";
> }
>
> ......
>
> return "firstName=" + res.getFirstName() + "{" +
>                  "lastName=" + res.getLastName() + ", " +
>                  "address=" + res.getAddress() + "/" + "***" + ", " +
>                  "zipCode=" + resource.getZipCode() + "}";

     This won't be "inefficient," since try/catch itself incurs very
little overhead: It's the building and throwing of the exceptions that
chews up resources, and if the various getXxx() calls are going to
build and throw them anyhow, rearranging the try/catch blocks won't
save much.  Besides, I don't think you *can* rearrange them to get
the effect you want, since the "recovery action" for each exception
differs depending on which method threw it.

     You could, I guess, write a little wrapper that takes a String-
yielding lambda expression, evaluates it, and either returns its
value or catches the exception and returns "" -- but if you're
trying to avoid clutter, that sounds like a step in the wrong
direction.

     My big question, though, is about the exception itself.  Why
in the name of all that's holy does a no-argument method throw an
exception that complains about an illegal argument?  Which one of
its non-existent arguments is it complaining about? Does the
complaint actually come from a lower-level method?  For example,
maybe getFirstName() looks like

	String getFirstName() {
	    return getAttribute("FIRST_NAME");
	}

... and it's really getAttribute() that complains.  If that's the
case, I'd suggest a small rewrite like

	String getFirstName() {
	    return getAttributeOrBlank("FIRST_NAME");
	}

	private String getAttributeOrBlank(String attr) {
	    try {
	        return getAttribute(attr);
	    } catch (IllegalArgumentException ex) {
	        return "";
	    }
	}

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Way to avoid repeating to catch the same exception in overridden method mike <mikaelpetterson@hotmail.com> - 2015-10-19 01:02 -0700
  Re: Way to avoid repeating to catch the same exception in overridden method Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-19 12:11 -0400
    Re: Way to avoid repeating to catch the same exception in overridden method Robert Klemme <shortcutter@googlemail.com> - 2015-10-23 08:59 +0200
  Re: Way to avoid repeating to catch the same exception in overridden method Martin Gregorie <martin@address-in-sig.invalid> - 2015-10-19 12:26 +0000
  Re: Way to avoid repeating to catch the same exception in overridden method Silvio <silvio@internet.com> - 2015-10-23 11:00 +0200
    Re: Way to avoid repeating to catch the same exception in overridden method Martin Gregorie <martin@address-in-sig.invalid> - 2015-10-23 10:32 +0000

csiph-web