Your SlideShare is downloading. ×
0
Lightning talk on java
Lightning talk on java
Lightning talk on java
Lightning talk on java
Lightning talk on java
Lightning talk on java
Lightning talk on java
Lightning talk on java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lightning talk on java

278

Published on

Good Java Programming Practices

Good Java Programming Practices

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • By context you mean, a string message? Sure, should do that cause it helps with debugging...
    Much of the details like this I omitted to keep focus on the idea that throw runtime only when the original exception is not runtime.

    The platform we are developing, we actually care a lot that exceptions are not swallowed. Surprisingly many senior developers would also do exactly opposite creating problems (and which are difficult to debug).
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • It is a very good slides deck. On the 2nd slide, in the case that we want to wrap non-RuntimeException Exception in a RuntimeException, we need to provide context so that it is easier to debug. Just 2 cents! :-)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
278
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
2
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Lightning Talk on Java Common Programming Oversights Chetan Narsude January 20th, 2014
  • 2. Don’t Swallow Exceptions • Mistake try { } catch (Exception e) { e.printStackTrace(); } • Bad try { } catch (Exception e) { logger.warn(“Exception”, e); } Recommended try { } catch (Throwable e) { if (e instanceof Error) { throw (Error)e; } if (e instanceof RuntimeException) { throw (Runtime)e; } • Ugly try { } catch (Exception e) { handleException(e); } /* somewhere else */ public void handleException(Exception e){} throw new RuntimeException(e); }
  • 3. Don’t wrap Throwable in RuntimeException try { try { … … } } catch (Throwable cause) { catch (Throwable cause) { throw new RuntimeException(cause); } DTThrowable.rethrow(cause); }
  • 4. Don’t Catch All Throwable try { try { … … } } catch (Throwable cause) { catch (Error error) { throw new RuntimeException(cause); } throw error; } catch (RuntimeException runtime) { throw runtime; } catch (Throwable cause) { throw new Runtime(cause); }
  • 5. Don’t Use Static Instance of SimpleDateFormat private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); … /* somewhere in the code */ ed = dateFormat.parse(dateInput.readUTF()); … /* somewhere in the code */ ed = new SimpleDateFormat("yyyy-MMdd").parse(dateInput.readUTF()); private static final ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat(“yyyy-MM-dd”); } } … /* somewhere in the code */ ed = dateFormat.get().parse(dateInput.readUTF());
  • 6. Synchronize Only on Final private Object mutex = new Object(); private final Object mutex = new Object(); /* somewhere in the code */ synchronized (mutex) { reload = true; mutex.notify(); } /* somewhere in the code */ synchronized (mutex) { reload = true; mutex.notify(); } /* possibly elsewhere in the code */ public void setMutex(Object mutex) { this.mutex = mutex; }
  • 7. Avoid Concatenation While Logging /* various ways of wrongly using logger */ logger.debug(“Exception = “ + e.getMessage()); logger.debug(“Exception = {}”, e); logger.debug(“Exception “ + id + “ “ + e.toString()); /* just print the message from exception */ logger.debug(“Exception = {}“, e.getMessage()); /* print the exception stack trace */ logger.debug(“Some Operation”, e); /* print templatized message with exception stack trace */ logger.debug(“Exception {}“, id, e);
  • 8. Prevent Resource Leak InputStream stream = new …Stream(); InputStream stream = new …Stream(); while (…) { try { process(stream); while (…) { } process(stream); stream.close(); } } finally { stream.close(); }

×