Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL
Java 6
MO NDA Y , MA R C H 25, 2013
Developers Java
When to make a method static in Java Top 5 JQuery books for Beginners and Web developers - Best of lot How SSL, HTTPS and Certificates works in Java web applications Role based Access control using Spring Security and MVC, Mapping LDAP Groups to Authorities for Authorization 10 Java Exception and Error Interview Questions Answers 5 must read books to learn Object Oriented and Java Design patterns - Best of lot
4) Always provide meaning full message on Exception message of Exception is the most important place, where you can point out cause of problem because this is the first place every programmer looks upon. Always try to provide precise and factual information here. For example, compare these two Exception messages for I l l e g a l A r g u m e n t E x c e p t i o n: m e s s a g e1 :" I n c o r r e c ta r g u m e n tf o rm e t h o d " m e s s a g e2 :" I l l e g a lv a l u ef o r$ { a r g u m e n t } :$ { v a l u e }
Follow Us
Follow @javinpaul 3,239 follow ers
Javarevisited on
first one just says that argument is illegal or incorrect, but second one include both name of argument and its illegal value which is important to point out cause of error. Always follow this Java b est practice, when writing code for handling exceptions and errors in Java.
Follow
+3,257
5) Avoid overusing Checked Exception Checked Exception has there advantage in terms of enforcement, but at same time it also litters the code and makes it unreadable by obscuring business logic. You can minimize this by not overusing checked Exception which result in much cleaner code. You can also use newer Java 7 features like one catch block for multiple exceptions and automatic resource management, to remove some duplication.
javarevisited.blogspot.in/2013/03/0-exception-handling-best-practices-in-Java-Programming.html
1/3
7/15/13
7) Remember Exceptions are costly in terms of performance One thing which is worth remembering is that Exceptions are costly, and can slow down your code. Suppose you have method which is reading from R e s u l t S e tand often throws S Q L E x c e p t i o nthan move to next element, will be much slower than normal code which doesn't throw that Exception. So minimizing catching unnecessary Exception and moving on, without fixing there root cause. Dont just throw and catch exceptions, if you can use boolean variable to indicate result of operation, which may result in cleaner and performance solution. Avoid unnecessary Exception handling by fixing root cause.
Javarevisited
8) Avoid empty catch blocks Nothing is more worse than empty c a t c hblock, because it not just hides the Errors and Exception, but also may leave your object in unusable or corrupt state. Empty catch block only make sense, if you absolutely sure that Exception is not going to affect object state on any ways, but still its better to log any error comes during program execution. This is not a Java best practice, but a most common practice, while writing Exception handling code in Java.
Like 4,660 people like Javarevisited.
9) Use Standard Exceptions Our ninth Java best practice advise on using standard and inbuilt Java Exceptions. Using standard Exception instead of creating own Exception every now and then is much better in terms of maintenance and consistency. Reusing standard exception makes code more readable, because most of Java developers are familiar with standard R u n t i m e E x c e p t i o n from JDK like, I l l e g a l S t a t e E x c e p t i o n ,I l l e g a l A r g u m e n t E x c e p t i o nor NullPointerException, and they will immediately be able to know purpose of Exception, instead of looking out another place on code or docs to find out purpose of user defined Exceptions. 10) Document Exception thrown by any method Java provides throw and throws keyword to throw exception and in javadoc you have @ t h r o wto document possible Exception thrown by any method. This becomes increasingly important if you are writing API or public interface. With proper documentation of Exception thrown by any method you can potentially alert anyone who is using it. That's all on Java best practices to follow while handing Exceptions in Java. Do let us know what practices you follow while writing Exception handling code in Java.
Recommendd Books Java Programming and Design Pattern Spring Framework Subscribe by email: Subscribe By Javin Paul Subscribe To This Blog Free Posts Comments
Related Java Best Practices tutorials from Javarevisited blog How to write Production Quality Code in Java Top 10 JDBC Best Practices for Java Programmers Code review Checklist and best practices 10 tips to follow while writing Code Comments in Java Java best Practices for overloading and overriding methods
Business Ideas
Moving Service
Performance Review
Difference between Wait and Sleep , Yield in Java Checked vs Unchecked Exception in Java Example How to use jdk7 multi-cache block with example Top 10 Collection Interview Questions Answers in Java
Recommended by
May (14) April (18) March (16) How to Convert and Print Byte array to Hex String ... 10 Exception handling Best Practices in Java Progr... How to Reverse Array in Java - Int and String Arra...
Programming Software
6 comments:
Anonymous said...
javarevisited.blogspot.in/2013/03/0-exception-handling-best-practices-in-Java-Programming.html
2/3
7/15/13
Good :) March 25, 2013 at 10:12 PM Anonymous said... Nice.... March 27, 2013 at 1:11 PM Anonymous said...
Nine out of ten are items in "Effective Java, Second Edition." If that's not just coincidence, if you were inspired by that book, it seems fair to give the authors credit. March 28, 2013 at 6:57 AM Javin @ Java Classloder Working said... @Anonymous, If that's the case, then it just a coincidence mate. Though I did read the book, and I am big fan of him, this post was mainly a collection of best practices, what I follow. To be fair, every one with few years of experience in Java knows about it, it's more Java programmer with couple of years experience. Having said, that I highly recommend Effective Java to any Java developer, you can also read it about in my post of 5 good books for Java Programmer March 28, 2013 at 6:54 PM Anonymous said... this one is worth a couple reads...good stuff, man April 1, 2013 at 11:00 PM Palak said... Hi Javin, Can you please share tips to write robust code, I mean code which handles exceptional scenario gracefully e.g. reading a malformed message if you are expecting a fixed length binary message, or an XML which doesn't has required XPATH . Most importantly how to find those exceptional scenario and how to decide action needs to be taken on that time? April 29, 2013 at 11:22 PM
Post a Comment
References E n t e ry o u rc o m m e n t . . . Java API documentation JDK 6 Spring framework doc Struts ANT Maven Comment as: Google Account Publish Preview JDK 7 API MySQL Linux Eclipse Copyright by Javin Paul 2012. Powered by Blogger.
Home
Older Post
About Me
Privacy Policy
javarevisited.blogspot.in/2013/03/0-exception-handling-best-practices-in-Java-Programming.html
3/3