Rate this page:
Randomly sorted guidelines.
Exception
unless that's all you're having thrown to you.Exception
ever.RuntimeException
as well.Throwable
if you want to continue breathing.Error
and any subclasses of Error
as well.NullPointerException
?@throws
Javadoc like you would a checked exception.Where possible create, document and throw meaningful unchecked exceptions. For example, write this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class MyGroupManager
{
/**
* ...
* @throws InvalidGroupException if the group cannot be handled
*/
public void handleGroup(Group group) throws InvalidGroupException
{
if (!isValidGroup(group))
throw new InvalidGroupException("Group is invalid: " + group.toString());
// do something with the group
}
}
public class InvalidGroupException extends RuntimeException
{
// ...
}
In preference to this:
1 2 3 4 5 6 7 8 9
public class EvilGroupManager
{
public void handleGroup(Group group)
{
if (!isValidGroup(group))
throw new RuntimeException("Group is invalid: " + group.toString());
// do something with the group
}
}
The latter implementation is not as good because it gives the calling code very little discretion as to what kind of exceptions it wants to handle.
Rate this page: