Tuesday, November 10, 2009

Exception Handling - Do's and Don’ts -Code for failures -Part-3


Part-1 is here...
Part-2 is here...




Do's and Don'ts in Exception Handling

1. Do not suppress the exception
2. Do catch the specific exception than the toplevel or generic exceptions
3. Do handle the thrown exception.
4. Do not throw the irrelevant exceptions to the caller
5. Do not handle the irrelevant exception by own.
6. Do Wrap-up the exception



1. Do not suppress the exception

try{

// File reading code here

}catch(IOException ioEx){

// do Nothing

}

Often we use try catch block in our code when compiler/IDE force as to handle the checked exceptions, we do write try catch block and just leave the catch block as
Empty, first of all we must handle these checked exceptions, that's the reason it has been forced by compilers, by leaving empty catch block, we defeat the purpose of these checked exceptions.

2. Do catch the specific exception than the top-level or generic exceptions

Catch the specific exceptions as much as possible; do not just catch the generic exceptions.

try{

// File reading code here

}catch(FileNotFoundException fnfEx){

// do something

}catch(IOException ioEx){

// do something

}catch(Exception ex){

// do something

}


3. Do handle the thrown exception.

Compiler won't force us to handle the unchecked exceptions, it doesn't mean we no need to handle, for the good exceptiona handling must handle all the exceptions as much as possible.


4. Do not throw the irrelevant / unknown exceptions to the caller

This is one of the important point in exception handling, code shouldn't throw all the exceptions to the caller, if any exception which can be handled by own, that exceptions must be handled then and there, for example, a method is trying to connect to Database and it throws Connection Could not established exception, then code should not throw this to handler, the method must handle and do some recoveries like re try or try to connect to the alternate Database. the caller may not know what to do with this exception, so this must be handled then and there.


5. Do not handle the irrelevant exception by own.

Like the above point, the code must not handle the exceptions which are not relevant to that piece, it should throw the exception to the caller.


6. Do Wrap-up the exception

From the bottom to top, if the code throws all the exceptions as it is, the top level must handle or know about too many exceptions, so must wrapper the exceptions, log down it and pass to the above layer.

For example, if DAO layer got some exception like SQLExcpetion, the end user or UI not necessarily say what is the error, instead this code wrap up as SystemException and pass it to the above layer, even though wrap that to system exception it should be logged down properly in the log files as SQLException, otherwise developer or operation people get confused about the exceptions.

Thursday, July 23, 2009

Exception Handling - Code for failures - Part-2

Part-1 is here...
Exception can be thrown for two reasons
1. Application failure, which includes operational failures, code issues and all other technical issues. (mostly non recoverable by end user)

2. Business validations. (mostly recoverable by end user by changing the input data)


If any failure happened,
we required below information to fix the issue

1. What was the failure?
2. Where it happened?
3. Which circumstance or state or environment?
4. What are the input data?
5. Is this recoverable?
6. Is it necessary to Alert / Escalate to the support people?


Our Exception handling must provide all these details whenever the failure happened.

How do we provide these information?

1. What was the failure?
Do not throw a generic high level exception, provide a more specific Exception class.

2. Where it happened?
Do not throw a generic high level exception, provide a more specific Exception class, if we use the Same exception class in many places differentiates with the Id or message. so we know exactly where the failure happened.

3. Which circumstance or state or environment?
This is very important, many time we programmers do not mind that our program work only in a Conditional state(or environment), so we need to provide this information while throwing exception by id orMessage.

The exception handler (typically a catch block or caller method) would change the state or look foralternate and re-try before throw the exception.

4. What are the input data?
This information is important for Exception handling, most of the time exception could happened by theinput data, throw proper data exception when the failure happened like NullArgumentException

5. Is this recoverable?
Most of the time, we throw Validation Exceptions, these validation exceptions would be recoverable by the end user, so the handling code must know that these exceptions are recoverable exceptions, so we Provide more meaningful information to recover this to the end user.

Secondly this would use to hold the same state or environment for the end user to continue working with the application.

6. Is it necessary to Alert / Escalate to the support people?
Not all the exceptions required admin or support people to be informed, so providing this information to the handler code will alert the admin or support team accordingly.

If we get all the mentioned information without seeing the log, code and trace is consider the better Exception handling.

The Exception handling tips Will be continued…

Tuesday, July 14, 2009

Fast Lane Reader Pattern - When to use this?

For our last few developments, we follow the Service --> Business --> DAO layer architecture.

We strictly follow that the Service layer can call only Business layer and not DAO, the same way Business layer can call only the DAO layer and not the Service layer, this is approach is very much OK for all the CRUD operations

But this approach is tedious, unnecessary and overhead for some straight forward operations like just to display large data without many validations, manipulations, business logic and so on… in this scenario we use Fast Lane Reader pattern. By this pattern we can call DAO layer directly from Service layer. So we avoid the complexity.

Conditions to use this

1. The data must be sued for Read only purpose and the data access also read only
2. It must be as a tabular form instead of Object form
3. Use only when the Data accuracy is not a crucial factor, means efficiency is more important than the current data.

We could achieve fast retrieval of large data…

We could avoid complexity in code, another way this will increase the design and code complexity by providing another passage to access the data.

Thursday, February 12, 2009

NUS Career Fair, SIngapore, Today and Tomorrow

NUS Career fair is held on to-day and to-morrow(12 and 13th Feb,2009) at Multi Purpose Sports Hall 1 & 2 .


Credit Suisse, AXA Life Insurance, JobStreet.com, JobsCentral, goverment agencies and many more companies participated in that... get the full list of the companies here

in many counters they are giving some free gifts like pen, note books and some more.

Monday, January 12, 2009

Exception Handling - Code for failures - Part-1

Exception Handling - Code for failures (Part-1)
This article is not about what is Exception Handling, this article is just providing some tips, do's , don'ts and sharing the knowledge about Exception Handling and designing of Exception Handling...


Exception Handling is nothing but preparation for failures, if we do not prepare for failures, then expect the more system failures.

Normally we the programmers do not bother much about failure cases while coding, programmers concentrate more on to code the funcationality, yes it is important to complete the optimistic path of the program but the world is not so perfect.
We write a program to run successfully in a conditionl environment, that means we define a program run successfully in this environemnt for this inputs and in this state, so we must concentrate on the Exception Handling to face when the program runs in improper environemt or inputs or states

Improper or poor exception handling will lead for the following things
1. Maitenance will be very difficult
2. Difficult to debug.
3. More turn around time.

Do we have any gauge for perfect Exception handling?
Never mind, as long as we are able to identify the reason for failures without looking the Stacktrace then the Exception Handling is done in good manner.

I will write more in the next part...

Part 2 is here

Wednesday, January 7, 2009

Core J2EE Patterns best practices and design strtegies e-book

Definition for Design Patterns in Wikipedia is
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Most of the time you are not the first person to face some problem in the world, some one might face this issue earlier, the solutions or method they find solution would be useful to resolve your problem. this is where patterns are important.

Design Patterns are not difficult to understand, I found the website javacomp.org explains the basics of Design pattern in a simple and easy to understand manner.

The above design pattern is common for OOAD, most of the time we look solutions on platform oriented, the sameway we need patterns for J2EE...

The Design Patterns Java Companion by James W. Cooper also simple to understand

I found that the book "Core J2EE Patterns best practices and design startegies" by Deepak Alur, John Crupi, Dan Malks is veryuseful.

you can download the softcopy of the book by clicking this link

In the book they mentioned abouot what this book is about and what this book is not about

What This Book is About?

This book is about:

• Using patterns for the J2EE Platform.
Based on our collective J2EE platform experience, we have assembled the
pattern catalog in this book. The J2EE Pattern Catalog describes various best
practices related to architecting and designing applications for the J2EE
platform. This book focuses on the following four J2EE technologies: Servlets,
JSP, EJB components, and JMS.

• Using best practices to design applications that use JSP, Servlet, EJB
components, and JMS technologies.

It is not sufficient to merely learn the technology and the APIs. It is equally
important to learn to design with the technology. We have documented what
we have experienced to be the best practices for these technologies.

• Preventing re-inventing-the-wheel when it comes to design and architecture
for the J2EE platform.
Patterns promote design reuse. Reusing known solutions reduces the cycle
time for designing and developing applications, including J2EE applications.

• Identifying bad practices in existing designs and refactoring these designs to
move to a better solution using the J2EE patterns.
Knowing what works well is good. Knowing what does not work is equally
important. We have documented some of the bad practices we have
experienced when designing applications for the J2EE platform.

What This Book Is Not?

This book is not about:
• How to program with Java or J2EE technologies
This book is not about programming. While this book is heavily based on the
J2EE technologies, we do not describe the specific APIs. If you wish to learn
about programming using Java or using any of the J2EE technologies, there
are a number of excellent books and online resources from which to learn.
The online tutorials on the official Java home page at http://java.sun.com
are highly recommended if you wish to learn about individual technologies.
The official specifications for J2EE technologies are also available from the
Java home page.

• What process and methodology to use
We do not suggest any type of process or methodology to use since the
material presented in this book is not related to either. Hence, this book does
not teach you about a process or methodology to follow in your projects. If
you would like to learn more about processes and methodologies, there are
a good number of books that deal with various object-oriented
methodologies and new books on lightweight processes, such as Extreme
Programming.

• How to use Unified Modeling Language (UML)
This book is not going to teach you about UML. We use UML extensively
(specifically class and sequence diagrams) to document the patterns and
describe the static and dynamic interactions. If you want to learn more about
UML, please refer to the UML User Guide [Booch] and the UML Reference
Manual [Rumbaugh] by Grady Booch, Ivar Jacobson and James Rumbaugh.

Sunday, December 28, 2008

Boost Performance with 5.1 and Query Analyzer

MySQL Live Webinar News


MySQL invites you to join our very exciting Live Webinar in January for Asia South. Space is limited, so register now!!

Title:Boost Performance with 5.1 and Query Analyzer (Asia South)

MySQL Live Webinar News


MySQL invites you to join our very exciting Live Webinar in January for Asia South. Space is limited, so register now!!

- Title:
Boost Performance with 5.1 and Query Analyzer (Asia South)
- Date:
Thursday, January 8, 2009
11:00 am in Singapore/Malaysia/Philippines
10:00 am in Bangkok/Indonesia/Vietnam
12:00 pm in Japan
- Presenter:
Robin Schumacher, Director of Product Management, Sun MySQL
Rob Young, Senior Manager of Product Management, Sun MySQL
- Register:
http://www.mysql.com/news-and-events/web-seminars/display-258.html


Join this webinar to learn how MySQL Enterprise now delivers higher performance for large-scale enterprise applications through Partitioning and Row-Based Replication - as well as the ability to further optimize database performance and manage scale with Query Analyzer; a must-have tool for DBAs and developers to identify and analyze problem SQL code causing performance issues and slow downs.


For recorded replays of our most popular live web seminars that cover a wide range of topics, please go to http://www.mysql.com/news-and-events/on-demand-webinars/.



Sincerely,

Sun Microsystems / MySQL
www.mysql.com
Thursday, January 8, 2009
11:00 am in Singapore/Malaysia/Philippines
10:00 am in Bangkok/Indonesia/Vietnam
12:00 pm in Japan
- Presenter:
Robin Schumacher, Director of Product Management, Sun MySQL
Rob Young, Senior Manager of Product Management, Sun MySQL
- Register:
http://www.mysql.com/news-and-events/web-seminars/display-258.html


Join this webinar to learn how MySQL Enterprise now delivers higher performance for large-scale enterprise applications through Partitioning and Row-Based Replication - as well as the ability to further optimize database performance and manage scale with Query Analyzer; a must-have tool for DBAs and developers to identify and analyze problem SQL code causing performance issues and slow downs.


For recorded replays of our most popular live web seminars that cover a wide range of topics, please go to http://www.mysql.com/news-and-events/on-demand-webinars/.