Hello World
I just had a funny idea (spleen) and wonder how the Java world would
think about it: Currently, Java offers a Exception handling mechanism
with "throw" and "try-catch-finally". Exceptions always lead to an
abort of the code that throwed the exception.
Makes sense.
But maybe sometimes a developer would like to let the method that
called his code decide whether or not the exception is that bad that
the process should be aborted. This is the case if the code would
principally be able to perform despite of the exception, but the
caller might not be interested in the result anymore.
My idea is that instead of an Exception, a Warning could be "thrown".
The receiver of the Warning catches it and either says "ignore", such
that the code is resumed from where the Warning was thrown, or
"abort". In the latter case, an Exception is thrown just as in the
classical mechanism.
Here is some code example of how it could work:
void someMethod() throws IllegalStateWarning, IllegalStateException {
// some code, then:
warn new IllegalStateWarning("...");
}
void someClient() {
try {
someMethod();
} catch (IllegalStateWarning w) {
if (someCriterion) {
System.out.println("Warning: " + w);
w.ignore();
} else {
w.abort();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
--- would be equivalent to --->
void someMethod(WarningListener l) throws IllegalStateException {
// some code, then:
// warn new IllegalStateWarning("...")
Warning w = new IllegalStateWarning("...")
l.warn(w);
if (w.aborted()) throw w.createException();
}
void someClient() {
try {
someMethod(new WarningListener() {
public void warn(Warning w) {
// catch (IllegalStateWarning w)
if (w typeof IllegalStateWarning) {
if (someCriterion) {
System.out.println("Warning: " + w);
w.ignore();
} else {
w.abort();
}
}
}
});
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
Cheers,
Daniel


|