Skip to main content

Treat Exceptions as first class objects.

No replies
vhi
Offline
Joined: 2004-10-11
Points: 0

At present, Java encourages ambigous and minimalistic error messages. For example, the ClassCastException does not specify the offending class. Exceptions in Java are dumb classes that contain a string message, which has to be parsed to get any meaningful elements. For example, if I connect to a non-existant port, then the ConnectException contains the port number in the message string, which has to be parsed. Since the purpose of an Exception is also to provide the user with more helpful information, why not provide the port number in a separate getter method?
Example:

----
public class ConnectException extends SocketException {
private int portNo_;
public ConnectException(String msg) {
super(msg);
}
public ConnectException(String msg, int portNo) {
super(msg);
portNo_ = portNo;
}
public ConnectException() {}
public int getPortNumber() {
return portNo_;
}
}
----

This style encourages developers to provide more useful information in the exception class itself.

This is more detailed in: http://c2.com/cgi/wiki?JavaExceptionsAreFirstClassObjects