Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-20-2010, 11:48 AM
Member
 
Join Date: Jan 2010
Posts: 6
Rep Power: 0
brodie is on a distinguished road
Default a new way to handle exceptions
well, i am working on a open source project named app4j, i designed this exception handling method, but i am not quite sure about it. so please give me some advice. the original post is here

1, use code to identify exception.
for app4j, there is only one exception type, the StandardException?(@see org.app4j.StandardException?) which is a runtime exception. codes are used to identify exceptions. one code one exception. and the exception information should be easily looked up by its code. this means app4j would easily support a global exception database. for development, i think iterate every exception is worthy. exceptions should be managed centrally.

2, register exception handlers for codes
when @Entry's attribute error set to true, the app4j would take the method as a exception handler, for example:

static final int ERROR_USER_EXISTS = 1000;
static final int ERROR_INVALID_INPUT = 1001;
@Entry(value="", error=true, codes={ERROR_USER_EXISTS, ERROR_INVALID_INPUT})
public void registerErrorHandler(Event e){
e.setForward("profile/register");
e.setErrorProcessed();
}
this method handles 2 exceptions, when register users, the user's name should be unique, so there is User exist exception, and it also handles user's invalid input error. if a code missing its handler, app4j has default handler to redirect request to error page.

3, replace checked exception to with a annotation @Error.
checked exceptions sometimes are necessary, especially for interfaces, if we omit a runtime exception, the program possibly crash. so if a interface method want to document its exception, in this way:

@Entry("register")
@Error({ERROR_USER_EXISTS})
public void register(Event e){
if(e.isReadonly()){
e.setForward("profile/register");
return;
}
String name = e.getParameter("name");
String password = e.getParameter("password");
String role = e.getParameter("role");

if(name != null && password != null && role != null){
if(profiles.get(name) != null){
e.setLastError(new StandardException(ERROR_USER_EXISTS, "User name exists"));
e.stop();
return;
}
Person p = new Person(e.getActor());
p.setName(name);
p.setPassword(password);
p.setRole(role);
this.profiles.put(p.getName(), p);

e.setAttribute(Actor.KEY, p, Event.Scope.SESSION);
e.setAttribute("user-name", p.getName());

e.setForward("profile/info");
}else{
if(name == null){
e.setLastError(new StandardException(ERROR_INVALID_INPUT, "User name can't be empty"));
}else if(password == null){
e.setLastError(new StandardException(ERROR_INVALID_INPUT, "User password can't be empty"));
}else if(role == null){
e.setLastError(new StandardException(ERROR_INVALID_INPUT, "User role can't be empty"));
}
e.stop();
}

}
with @Error, we specify the exception codes the method probably thrown, and app4j would look up the handler for the exception, if the handler is missing, the app would failed to install. in fact, for the next version of app4j, actions would compile at runtime, so the error handler would weave into the action.
@Error also supports java doc.

4, avoid try{...}catch(){...}.
App4j is based on event model, event popping from one action to another. when current action finds the execution preconditions are not satisfied, it will change the status of event, not throw a exception, the framework would detect that and stop event popping.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-20-2010, 11:57 AM
Senior Member
 
Join Date: Aug 2009
Posts: 1,895
Rep Power: 2
r035198x is on a distinguished road
Default
What are the goals of the project?
How do I do finally?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-20-2010, 01:16 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
Where are your exception ids defined?

In general, and I don't know the goals of this thing, having a single exception that holds an exception id is Bad Practice, in that it doesn't utilise any of the existing exception handling mechanism.

What problem with exceptions is this trying to solve?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-20-2010, 03:45 PM
Member
 
Join Date: Jan 2010
Posts: 6
Rep Power: 0
brodie is on a distinguished road
Default
well, where to define the ids is a big problem. i am thinking on it.
i am building a project based on OSGi, and i am trying to adapt the other open source projects to bundles to make them work together, like jforum, jspwiki. so i need a universal exception handling method.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-20-2010, 03:55 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
Why?
Why do you need a universal mechanism?
Nothing you do will manage to wire up all the thousands and thousands of exceptions that exist out there into one id-based exception system.

OSGi has an exception handling system doesn't it?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-20-2010, 03:56 PM
Member
 
Join Date: Jan 2010
Posts: 6
Rep Power: 0
brodie is on a distinguished road
Default
i should not post codes here, let's just focus on the design.
the design is following 3 rules:
1, use a single runtime exception and error code replacing throwing different kind of exception.
2, avoid using try...catch, register exception handler for every error code.
3, use an annotation @Error, to make a method 's thrown exception must be handled and could be documented.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-20-2010, 04:01 PM
Member
 
Join Date: Jan 2010
Posts: 6
Rep Power: 0
brodie is on a distinguished road
Default
if i adapt the other projects, i believe it would be a complex system, so error code makes the exceptions manageable and traceable , i believe big systems are using this way, like windows, mysql. and error code is easy to make the system i18n.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-20-2010, 04:07 PM
Member
 
Join Date: Jan 2010
Posts: 6
Rep Power: 0
brodie is on a distinguished road
Default
in fact i am trying to separate error handling and business logic, i hope the final solution is create a central error database, and use xml to map the error handling. when website admin or developers encountered a exception, he could lookup the error database by code and find information there.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-20-2010, 04:20 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
Why do you want to remove try/catch?
Error handling is a part of business logic.
An id based exception mechanism makes for harder to follow code, as you no longer have nice simple exception classes that (hopefully) describe the problem encountered in their name.

I really don't see an id-based system being any easier with regards to i18n...it's all resource bundles anyway.

And finally...as mentioned above. How do you handle "finally"?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-20-2010, 05:01 PM
Member
 
Join Date: Jan 2010
Posts: 6
Rep Power: 0
brodie is on a distinguished road
Default
thanks for your reply,
i think a typical java web application could be layered as Action, Service, Dao.
and exceptions could be categorized as recoverable and unrecoverable,
if we want the system to be neat and tidy, we need to define which layer to throw exception and which layer to handle it and how,
for the unrecoverable ones which including java.lang.Error, these exceptions could be only logged or redirect to error page, like database server is down.
i think those exceptions are not part of business logic. but as you said the recoverable exception handling indeed is part of business logic.
so here i made this assumption, most web application exceptions are unrecoverable, log and redirect to error page are the only way to handle them.

i am avoiding use try/catch, so i could use a declarative way to handle the unrecoverable exception.
about finally, i believe its usage mostly related to IO, it should in Dao layer, the new exception handling method would apply for Action and service layer.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-20-2010, 05:50 PM
Senior Member
 
Join Date: Apr 2009
Posts: 944
Rep Power: 1
Tolls is on a distinguished road
Default
Why not then go the Spring/Hibernate route and simply go for RuntimeExceptions then?

I'm really having a hard time understanding what problem this is attempting to solve...
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
app4j, exception

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handle sound varsamakos AWT / Swing 4 12-26-2009 10:40 PM
how to handle files? sayan751 Advanced Java 5 06-10-2009 09:39 PM
handle FileNotFoundException minifish New To Java 13 11-08-2008 03:01 AM
Better way to handle exceptions javaplus Advanced Java 2 01-16-2008 07:47 PM
how to handle exceptions paty Advanced Java 2 08-05-2007 05:17 AM


All times are GMT +2. The time now is 04:30 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org