AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What exception to use with invalid data fields on object pas

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.developer
View previous topic :: View next topic  
Author Message
davout
Guest





PostPosted: Mon May 07, 2007 7:06 pm    Post subject: What exception to use with invalid data fields on object pas Reply with quote



I have a question about what the convention is for throwing exceptions on
validating data passed to business tier objects.

If I have a business tier service like where 'AccountManager', a business
tier that maintains accounts...

public class Account {
...
private String fTitle;
private String fAccountCode
....
}

public class AccountManager {
...
public void addAccount(Account aNewAccount);
...
}

.... and any new account must have a non null title and a non-null account
code.

If the 'aNewAccount' parameter is passed with a null title field or a null
account code field what type of exception should I throw? Should the
business tier code be throwing an IllegalArgumentException? If so, how can
my GUI tier determine what field failed the validation test? Or should I use
a separate custom exceptions for each validation point? One custom exception
for title, another for alias etc.
Back to top
Thomas Fritsch
Guest





PostPosted: Mon May 07, 2007 7:46 pm    Post subject: Re: What exception to use with invalid data fields on object Reply with quote



davout wrote:

Quote:
I have a question about what the convention is for throwing exceptions on
validating data passed to business tier objects.

If I have a business tier service like where 'AccountManager', a business
tier that maintains accounts...

public class Account {
...
private String fTitle;
private String fAccountCode
....
}

public class AccountManager {
...
public void addAccount(Account aNewAccount);
...
}

... and any new account must have a non null title and a non-null account
code.
Given your requirement above, I think the natural place for these checks

would be the the Account constructor (and its setTitle/setAccountCode
methods, if there are such) rather than the AccountManager:
public class Account {
...
public Account(String title, String accountCode) {
if (title == null)
throw new IllegalArgumentException("title is null");
if (accountCode == null)
throw new IllegalArgumentException("accountCode is null");
fTitle = title;
fAccount = accountCode;
}
...
}

Quote:

If the 'aNewAccount' parameter is passed with a null title field or a null
account code field what type of exception should I throw? Should the
business tier code be throwing an IllegalArgumentException? If so, how
can my GUI tier determine what field failed the validation test? Or should
I use a separate custom exceptions for each validation point? One custom
exception for title, another for alias etc.
I personally would prefer IllegalArgumentException for all (with different

detail messages), instead of lots of custom exception classes.

--
Thomas
Back to top
Mich
Guest





PostPosted: Mon May 07, 2007 7:47 pm    Post subject: Re: What exception to use with invalid data fields on object Reply with quote



"davout" <davoutXXXYYYWWWZZZ (AT) dsl (DOT) pipex.com> wrote in message
news:L7-dnT7iDP7Dr6LbnZ2dnUVZ8vqdnZ2d (AT) pipex (DOT) net...
Quote:
I have a question about what the convention is for throwing exceptions on
validating data passed to business tier objects.

If I have a business tier service like where 'AccountManager', a business
tier that maintains accounts...

public class Account {
...
private String fTitle;
private String fAccountCode
....
}

public class AccountManager {
...
public void addAccount(Account aNewAccount);
...
}

... and any new account must have a non null title and a non-null account
code.

If the 'aNewAccount' parameter is passed with a null title field or a null
account code field what type of exception should I throw? Should the
business tier code be throwing an IllegalArgumentException? If so, how
can my GUI tier determine what field failed the validation test? Or should
I use a separate custom exceptions for each validation point? One custom
exception for title, another for alias etc.


Why not create your own exception, such as NullAccountField with a different
message for each field?
Back to top
Lew
Guest





PostPosted: Tue May 08, 2007 1:53 am    Post subject: Re: What exception to use with invalid data fields on object Reply with quote

davout wrote:
Quote:
I have a question about what the convention is for throwing exceptions on
validating data passed to business tier objects.
If the 'aNewAccount' parameter is passed with a null title field or a null
account code field what type of exception should I throw? Should the
business tier code be throwing an IllegalArgumentException? If so, how
can my GUI tier determine what field failed the validation test? Or should
I use a separate custom exceptions for each validation point? One custom
exception for title, another for alias etc.

Thomas Fritsch wrote:
Quote:
I personally would prefer IllegalArgumentException for all (with different
detail messages), instead of lots of custom exception classes.

I concur with Thomas, although there really is no well-established convention.
The best practice is to use the exception that makes the most sense, which
Thomas's sure seems to do. You ask for an exception that indicates that an
argument is illegal, and Hey, presto! java.lang.IllegalArgumentException
provides exactly that, as an unchecked Exception.

There is an argument to use NullPointerException if the null-pointerness is
what's most important to you, as opposed to IllegalArgumentException if the
illegal-argumentness is what's most significant.

In the world of RuntimeExceptions there's almost always a suitable one
available in the standard API, usually from java.lang.

For checked Exceptions there's a better case for writing (usually only) one
custom, application-specific Exception that uses any underlying Throwable as
its cause. Creating a hierarchy of custom (checked) Exceptions is possible
but to my mind offers little to negative benefit.

The question of whether to throw checked or unchecked Exceptions is subtler.
Think as an API designer when pondering it.

--
Lew
Back to top
Wojtek
Guest





PostPosted: Tue May 08, 2007 3:29 am    Post subject: Re: What exception to use with invalid data fields on object Reply with quote

Thomas Fritsch wrote :
Quote:
I personally would prefer IllegalArgumentException for all (with different
detail messages), instead of lots of custom exception classes.

Except where you want to control which exception gets handled where.

The alternative is to parse the exception message (or custom flag) and
if you do not want to handle it there, re-throw the exception. Not as
clean as a custom exception class.

--
Wojtek Smile
Back to top
Reinder Verlinde
Guest





PostPosted: Sat May 19, 2007 3:25 am    Post subject: Re: What exception to use with invalid data fields on object Reply with quote

In article <mn.3ba17d751a41a264.70216 (AT) a (DOT) com>, Wojtek <nowhere (AT) a (DOT) com>
wrote:

Quote:
Thomas Fritsch wrote :
I personally would prefer IllegalArgumentException for all (with different
detail messages), instead of lots of custom exception classes.

Except where you want to control which exception gets handled where.

In that case, you can do:

class myIllegalArgumentException extends IllegalArgumentException {
...
}

...


try {
...
} catch( myIllegalArgumentException e) {
...
} catch( IllegalArgumentException e) {
...
}

Reinder
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.developer All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.