Exceptions First "Both optimists and pessimists contribute to society. The optimist invents the airplane, the pessimi...
Exceptional Cases <ul><li>This CANNOT be covered in requirements, its developers' sole responsibility </li></ul><ul><li>Fo...
Tom, JK & Harry’s codes <ul><li>try { </li></ul><ul><li>//do something; </li></ul><ul><li>} catch (IOException e) { </li><...
Tom, JK & Harry’s codes <ul><li>try { </li></ul><ul><li>//do something; </li></ul><ul><li>} catch (IOException e) { </li><...
Tom, JK & Harry’s codes <ul><li>try { </li></ul><ul><li>return something; </li></ul><ul><li>} catch (IOException e) { </li...
Tom, JK & Harry’s codes <ul><li>if(something wrong){ </li></ul><ul><ul><li>throw new  Exception (“Error”); </li></ul></ul>...
Unchecked Vs Checked Exceptions <ul><li>Unchecked exceptions represent conditions that, generally speaking, reflect errors...
Unchecked Exceptions:  Prevent Them <ul><ul><ul><li>private static int mod(int a, int b) {  </li></ul></ul></ul><ul><ul><u...
Unchecked Exceptions:  Prevent Them <ul><li>private static int mod(int a, int b) { </li></ul><ul><li>try { </li></ul><ul><...
Unchecked Exceptions:  Prevent Them <ul><li>private static int mod(int a, int b) { </li></ul><ul><li>try { </li></ul><ul><...
Unchecked Exceptions:  Prevent Them <ul><li>private static int mod(int a, int b) {  </li></ul><ul><ul><li>if(0 == b){ </li...
Unchecked Exceptions:  Prevent Them <ul><li>public static void main(String[] args) { </li></ul><ul><li>int a = 1; </li></u...
Checked exception:  don't handle, but propagate  back <ul><li>public boolean isAvailable(String id) throws PipeException{ ...
Checked exception:   You handle it, Your Responsibility <ul><li>try {  </li></ul><ul><li>if (condition) { </li></ul><ul><l...
So Simple Haa? <ul><li>Its simple, but we don't do simple things simple! </li></ul><ul><li>Kinda people who ship a system ...
Contd... <ul><li>Propagating Checked Exceptions across modules creates coupling </li></ul><ul><ul><li>Always catch and re ...
Contd… <ul><li>protected ModelAndView processFinish(HttpServletRequest httpServletRequest, HttpServletResponse httpServlet...
Use Finally for cleansing <ul><li>public void dataAccessCode() { </li></ul><ul><li>Connection conn = null; </li></ul><ul><...
Good Practices <ul><li>Use error logging effectively </li></ul><ul><ul><li>Genuine or Not, its our support going to strugg...
WORRY ABOUT EXCEPTIONS <ul><li>Decide which method to handle and which one to propagate (Throw early catch late ) </li></u...
DANKE!
Upcoming SlideShare
Loading in …5
×

Exceptions irst

879 views

Published on

  • Be the first to comment

  • Be the first to like this

Exceptions irst

  1. 1. Exceptions First &quot;Both optimists and pessimists contribute to society. The optimist invents the airplane, the pessimist invents the parachute.&quot; — George Bernard Shaw
  2. 2. Exceptional Cases <ul><li>This CANNOT be covered in requirements, its developers' sole responsibility </li></ul><ul><li>For a single happy scenario, You may need 10 erroneous scenarios, which makes your effort 11 times bigger than what you think. </li></ul><ul><li>Always fear about the failure during the development. What if </li></ul><ul><ul><li>the input becomes null? </li></ul></ul><ul><ul><li>this transaction failed? Should we roll back? </li></ul></ul><ul><ul><li>this checked exception happens? Should I handle it? Or propagate back? </li></ul></ul><ul><ul><li>There is a runtime exception? Can the system handle? Who builds the system anyway? </li></ul></ul>
  3. 3. Tom, JK & Harry’s codes <ul><li>try { </li></ul><ul><li>//do something; </li></ul><ul><li>} catch (IOException e) { </li></ul><ul><li>e.printStackTrace(); </li></ul><ul><li>} </li></ul>
  4. 4. Tom, JK & Harry’s codes <ul><li>try { </li></ul><ul><li>//do something; </li></ul><ul><li>} catch (IOException e) { </li></ul><ul><li>//This is it </li></ul><ul><li>} </li></ul>
  5. 5. Tom, JK & Harry’s codes <ul><li>try { </li></ul><ul><li>return something; </li></ul><ul><li>} catch (IOException e) { </li></ul><ul><li>return “”; </li></ul><ul><li>} </li></ul>
  6. 6. Tom, JK & Harry’s codes <ul><li>if(something wrong){ </li></ul><ul><ul><li>throw new Exception (“Error”); </li></ul></ul><ul><li>} </li></ul>
  7. 7. Unchecked Vs Checked Exceptions <ul><li>Unchecked exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time </li></ul><ul><ul><li>Do not handle the exception, but prevent it </li></ul></ul><ul><ul><li>There are exceptions for this! </li></ul></ul><ul><ul><li>Subclasses of ‘RuntimeException’ </li></ul></ul><ul><li>Checked exceptions caused due to genuine error scenarios happened outside the control of the system </li></ul><ul><ul><li>Connection Issues </li></ul></ul><ul><ul><li>Input Issues </li></ul></ul><ul><ul><li>Resourcing Issues </li></ul></ul><ul><ul><li>Subclasses of ‘Exception’ </li></ul></ul>
  8. 8. Unchecked Exceptions: Prevent Them <ul><ul><ul><li>private static int mod(int a, int b) { </li></ul></ul></ul><ul><ul><ul><li>return a % b; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>Is it correct? </li></ul>
  9. 9. Unchecked Exceptions: Prevent Them <ul><li>private static int mod(int a, int b) { </li></ul><ul><li>try { </li></ul><ul><li>return a % b; </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><ul><li>logger.error(“Error in calculating mod function”, e) </li></ul></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Is it Correct? </li></ul>
  10. 10. Unchecked Exceptions: Prevent Them <ul><li>private static int mod(int a, int b) { </li></ul><ul><li>try { </li></ul><ul><li>return a % b; </li></ul><ul><li>} catch (Throwable e) { </li></ul><ul><ul><li>logger.error(“Error in calculating mod function”, e) </li></ul></ul><ul><li>throw new IllegalArgumentException(&quot;Divisior can not be zero for mod operation&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Is it correct? </li></ul>
  11. 11. Unchecked Exceptions: Prevent Them <ul><li>private static int mod(int a, int b) { </li></ul><ul><ul><li>if(0 == b){ </li></ul></ul><ul><li>throw new IllegalArgumentException(&quot;Divisior can not be zero for mod operation&quot;); </li></ul><ul><li>} </li></ul><ul><li>return a % b; </li></ul><ul><li>} </li></ul><ul><li>Unchecked exception is prevented </li></ul><ul><li>The handling part of exceptional case is not the responsibility of the method </li></ul><ul><li>Its caller’s responsibility to handle zero cases </li></ul>
  12. 12. Unchecked Exceptions: Prevent Them <ul><li>public static void main(String[] args) { </li></ul><ul><li>int a = 1; </li></ul><ul><li>int b = 1; </li></ul><ul><li>//read a </li></ul><ul><li>//read b </li></ul><ul><li>if(0 == b){ </li></ul><ul><li>// tell user the number is not acceptable </li></ul><ul><li>} </li></ul><ul><li>int mod = mod(a, b); </li></ul><ul><li>} </li></ul><ul><li>Its over to user at the end! </li></ul>
  13. 13. Checked exception: don't handle, but propagate back <ul><li>public boolean isAvailable(String id) throws PipeException{ </li></ul><ul><li>Object a = query(id); </li></ul><ul><li>return null != a; </li></ul><ul><li>} </li></ul><ul><li>private Object query(String id) throws PipeException; </li></ul><ul><li>It not isAvailable method's responsibility </li></ul>
  14. 14. Checked exception: You handle it, Your Responsibility <ul><li>try { </li></ul><ul><li>if (condition) { </li></ul><ul><li>throw new AppNotAvailableException(&quot;The recipient is null&quot;, </li></ul><ul><li>AppNException.INVALID_SHORTCODE); </li></ul><ul><li>} </li></ul><ul><li>//do something </li></ul><ul><li>targetService.execute(sdpRequestMessage, sdpMessageContext); </li></ul><ul><li>} catch (AppNotAvailableException e) { </li></ul><ul><li>sdpMessageContext = new SdpMessageContextImpl(); </li></ul><ul><li>sdpMessageContext.setShortcode(shortcode); </li></ul><ul><li>e.setShortcode(shortcode); </li></ul><ul><li>exceptionHandlerService.onMessagingException(sdpRequestMessage, sdpMessageContext, e); </li></ul><ul><li>} catch (Throwable th) { </li></ul><ul><li>exceptionHandlerService.onMessagingException(sdpRequestMessage, sdpMessageContext, th); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  15. 15. So Simple Haa? <ul><li>Its simple, but we don't do simple things simple! </li></ul><ul><li>Kinda people who ship a system if it compiles! </li></ul><ul><li>Kinda people who touch our code even when TCs failing </li></ul><ul><li>Some frameworks make simple things complex </li></ul><ul><ul><li>ORM(Spring, hibernate, JPA) exceptions are RUNTIME: Why? </li></ul></ul><ul><ul><li>But most of us don't know why! Result? </li></ul></ul><ul><ul><ul><li>DB Connection Error at UI. </li></ul></ul></ul><ul><ul><ul><li>System at stalemate when a integrity violation happens. </li></ul></ul></ul><ul><ul><ul><li>Genuine errors causing so many troubles. </li></ul></ul></ul><ul><ul><li>Some developers even don't know there can be ORM exception! </li></ul></ul>
  16. 16. Contd... <ul><li>Propagating Checked Exceptions across modules creates coupling </li></ul><ul><ul><li>Always catch and re throw module specific exception </li></ul></ul><ul><ul><li>public void delete(String appId) throws QueryByTransactionIdException { </li></ul></ul><ul><li>try { </li></ul><ul><li>jdbcTemplate.update(query); </li></ul><ul><li>} catch (DataAccessException e) { </li></ul><ul><li>throw new QueryByTransactionIdException(&quot;Error while adding the TransactionStatus&quot;, e); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 17. Contd… <ul><li>protected ModelAndView processFinish(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object command, BindException bindException) { </li></ul><ul><li>//do something </li></ul><ul><li>try { serviceLocator.getAmaFieldTypeConfigurationService().addNewAmaFieldType(amafieldType); </li></ul><ul><li>return new ModelAndView(successView); </li></ul><ul><li>} catch (MetadataException dae) { </li></ul><ul><li>getServiceLocator().getMetadataServiceManagerStub().reInitMBean(); </li></ul><ul><li>bindException.reject(&quot;globle.error.meta.access.exception&quot;); </li></ul><ul><li>bindException.getModel(); </li></ul><ul><li>return showPage(httpServletRequest, bindException, 0); </li></ul><ul><li>} catch (Throwable dae) { </li></ul><ul><li>bindException.reject(&quot;globle.error.unexcepected.exception&quot;); </li></ul><ul><li>bindException.getModel(); </li></ul><ul><li>return showPage(httpServletRequest, bindException, 0); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  18. 18. Use Finally for cleansing <ul><li>public void dataAccessCode() { </li></ul><ul><li>Connection conn = null; </li></ul><ul><li>try { </li></ul><ul><li>conn = getConnection(); </li></ul><ul><li>//some code that throws SQLException </li></ul><ul><li>} catch (SQLException ex) { </li></ul><ul><li>throw new JKException(&quot;Error while accessing data &quot;, ex) </li></ul><ul><li>} finally { </li></ul><ul><li>try { </li></ul><ul><li>DBUtil.closeConnection(conn); </li></ul><ul><li>} </li></ul><ul><li>catch (Throwable e) { </li></ul><ul><li>//do nothing or throw RuntimeException </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  19. 19. Good Practices <ul><li>Use error logging effectively </li></ul><ul><ul><li>Genuine or Not, its our support going to struggle. </li></ul></ul><ul><ul><li>logger.error(“Put some valuable message”, e) </li></ul></ul><ul><li>Always catch Throwable at the end </li></ul><ul><ul><li>Trust nobody including you </li></ul></ul><ul><ul><li>Handle the checked exception and the keep a catch clause for Throwable INCASE </li></ul></ul><ul><li>Sending traps: Be proactive & sensible </li></ul><ul><li>Keep the exception class directly under root package of the module </li></ul><ul><li>Log exception only once </li></ul>
  20. 20. WORRY ABOUT EXCEPTIONS <ul><li>Decide which method to handle and which one to propagate (Throw early catch late ) </li></ul><ul><li>Think about the complete flow with the exception and handle it at the right place </li></ul><ul><li>Write testcases for exceptional cases </li></ul><ul><li>Spend an hour to work on the UIs with exceptional scenarios </li></ul><ul><ul><li>Error code mapping to error pages </li></ul></ul><ul><ul><li>Exception class mapping to error pages </li></ul></ul><ul><ul><li>Error message resource bundles </li></ul></ul><ul><ul><li>What happens to session when this happen? </li></ul></ul><ul><ul><li>Should we enable back button? </li></ul></ul><ul><ul><li>Should we keep a home button? </li></ul></ul>
  21. 21. DANKE!

×