Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32900
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Martin Gregorie <martin@address-in-sig.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Way to avoid repeating to catch the same exception in overridden method |
| Date | Mon, 19 Oct 2015 12:26:58 +0000 (UTC) |
| Organization | A noiseless patient Spider |
| Lines | 61 |
| Message-ID | <n02nih$brt$1@dont-email.me> (permalink) |
| References | <fd943245-c84e-4b93-9ed5-e18fb25e256a@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Mon, 19 Oct 2015 12:26:58 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="12157"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18D5+Zh/bgYHKfIBicUUFEZKsh/nc4lGk0=" |
| User-Agent | Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) |
| Cancel-Lock | sha1:v2rgzqdK9RheEykNLMZr6kDg+1I= |
| Xref | csiph.com comp.lang.java.programmer:32900 |
Show key headers only | View raw
On Mon, 19 Oct 2015 01:02:07 -0700, 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:
>
I'd probably use a StringBuilder:
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("firstName=");
sb.append((res.getFirstName() == null ? "" : res.getFirstName());
sb.append("{lastName=");
sb.append((res.getLastName() == null ? "" : res.getLastName()));
sb.append(", address=");
sb.append((res.getAddress() == null ? "" : res.getAddress());
sb.append("/***, zipCode="));
sb.append((resource.getZipCode() == null ? "" : resource.getZipCode());
sb.append("}")
return sb.toString();
}
or a String.format() method:
public String toString() {
String s =
s.format("firstName=%s{lastName=%s, address="%s/***, zipCode=%s}",
(res.getFirstName() == null ? "" : res.getFirstName(),
(res.getLastName() == null ? "" : res.getLastName(),
(res.getAddress() == null ? "" : res.getAddress(),
(resource.getZipCode() == null ? "" : resource.getZipCode());
return s;
}
both using the conditional operator to avoid try..catch constructs.
These may not be more efficient, but are certainly easier to read and
more concise, particularly the String.format() statement.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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