123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

6 Tips To Improve Your Exception Handling

By Author: siyaram ray
Total Articles: 428

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.
An exception can occur for many different reasons. Following are some scenarios where an exception occurs.
• A user has entered an invalid data.
• A file that needs to be opened cannot be found.
• A network connection has been lost in the middle of communications or the JVM has run out of memory.
Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java programming.
• Checked exceptions − A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.

1. Use a single, system-wide exception class
Instead of creating separate classes for each exception type, create just one. And make it extend RuntimeException. This will reduce your class count and remove the need to declare exceptions you aren’t going to handle anyway.
I know what you’re thinking: How will I tell exceptions apart if they’re all the same type? And how will I track type-specific properties? Read on!
2. Use enums for error codes
Most of us were trained to put the cause of an exception into its message. This is fine when reviewing log files (ugh), but it does have drawbacks:
1. Messages can’t be translated (unless you’re Google).
2. Messages can’t be easily mapped to user-friendly text.
3. Messages can’t be inspected programmatically.
Putting info in the message also leaves the wording up to each developer, which can lead to different phrases for the same failure.

A better approach is to use enums to indicate the exception’s type. Create one enum for each category of errors (payments, authentication, etc.). Make the enums implement an ErrorCode interface and reference it as a field in the exception.
When throwing exceptions, simply pass in the appropriate enum.

Now when you need to test for a specific case, just compare the exception’s code with the enum.
3. Add error numbers to enums
In some cases a numerical error code can be associated with each exception. HTTP responses for example. For those cases, add a getNumber method to the ErrorCode interface and implement it in each enum. learn java tutorial

Numbering can be globally unique across all enums or each enum can be responsible for numbering itself. You can even use the implicit ordinal() method or load numbers from a file or database.
4. Add dynamic fields to your exceptions
Good exception handling means also recording relevant data, not just the stack trace. Doing this will save you big time when trying to diagnose and reproduce errors. And customers won’t have to tell you what they were doing when your app stopped working (you’ll already know and hopefully have fixed it).
The easiest way to do this is to add a java.util.Map field to the exception. The new field’s job will be to hold all your exception related data by name. You’ll also need to add a generic setter method following the fluent interface pattern.

5. Prevent unnecessary nesting
Long, redundant stack traces help no one. Even worse, they waste your time and resources. When rethrowing exceptions, call a static wrap method instead of the exception’s constructor . The wrap method will be responsible for deciding when to nest exceptions and when to just return the original instance.
6. Use a central logger with a web dashboard
Consider this tip a bonus. Depending on your situation, getting access to production logs could be quite a hassle. A hassle that may involve multiple go-betweens (since many developers don’t have access to production environments).
Things get worse if you’re in a multi-server environment. Finding the right server — or determining that the problem only affects one server — can be quite a headache.
My recommendations are:
1. Aggregate your logs in a single place, preferably a database.
2. Make that database accessible from a web browser.
There are many ways to do this and may products to choose from: log collectors, remote loggers, JMX agents, system monitoring software, etc. You can even build it yourself. The main thing is that you do it soon. Once you have it, you’ll be able to:
• Troubleshoot issues in a matter of seconds.
• Have a URL for each exception that you can bookmark or email around.
• Enable your support staff to determine root causes without involving you.
• Prevent testers from creating multiple tickets for the same bug. Plus they’ll have an exception URL to put in their ticket.
• Save money for your business.
• Keep your weekend and reputation intact.

More About the Author

We offer innovative and customized Java Training in Bangalore. Best Java course in Bangalore with Certified Experts. Go to free demo classes, get prepared by 8yrs experienced tutors and furthermore get hands on live project. Visit: http://infocampus.co.in/java-training-bangalore.html

Total Views: 116Word Count: 805See All articles From Author

Computer Programming Articles

1. Benefits Of Selenium - Test Automation Tool
Author: Siyaram ray

2. Designing And Execution Of Selenium Test Automation Framework
Author: Siyaram ray

3. Microsoft Access Queries And How Important They Are In Your Database
Author: Ben Beitler

4. Восхождение На Вулкан Ринджани. Ломбок. Индонезия (rinjani)
Author: Armando Kozak

5. The Future Of Innovation In The Era Of Artificial Intelligence
Author: Charlotte

6. Product Engineering Services From Enlume Inc
Author: Srikanth reddy

7. Www Mcafee Com Activate | Mcafee Com/activate | Is Mcafee Antivirus Not Functioning Properly?

8. 123.hp Setup | 123.hp Com/setup | 123.hp Setup Officejet
Author: setup desk

9. Best Software Training And Placement Company In Hyderabad
Author: hema sri

10. Professional Private Investigator Milwaukee Services To Unravel The Truth
Author: Milwforensics

11. Selenium Testing - Star Of Software Testing
Author: Siyaram ray

12. Android Application Development Services
Author: it science projects

13. Web Crayons Biz : Website, Mobile Development And Digital Marketing Company
Author: Skyler

14. Automation Testing – Selenium Supports For Languages Different Browsers And Platforms
Author: Siyaram ray

15. How Can I Find The Best Full Stack Developers In New York?
Author: denise cole

Login To Account
Login Email:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: