leblancmeneses
August 7th, 2009, 02:45 AM
I am working with another developer on some code and received an email that concerns exception handling in multiple languages.
> But I don't think we should introduce another exception for this, as
> it will make the signatures more bloated than they already are (in the
> Java version there are already two kinds of exceptions that must be
> propagated up, parsing exceptions and IO exceptions),
his concern is across:
public abstract boolean isMatch() throws IOException, ParsingFatalTerminalException;
protected boolean Predicate()
throws IOException, ParsingFatalTerminalException
and many more methods that look similar in both java and c++ as signatures define exceptions handled in those languages.
here is my reply:
To do's with exception:
raise specific exceptions to what the problem is.
DivideByZeroException .. there are reasons why the base class ArithmeticException is not thrown.
This has to do with making use of catch blocks and including more specific information about the exception.
A user should never parse an exception string to access exception data. (big no no) By using the correct type you have easy access to extra information you need from the exception.
However I do believe that our exceptions should inherit from the same base type: ModelException This way if the caller can expect base class ModelException and apply a catch block that will catch all child classes.
try
{
Boolean test = parser.IsMatch();
}
catch(ModelException e) {
//will catch parent and all childs that inherited form ModelException
}
//any others will be unhandled
Now if we expected other exceptions from occurring in c# and wanted to provide the caller a type to expect then the following could be done:
new SpecificExceptionName("exception message", innerException);
But this requires the caller to unravel the exception tree to access the cause of the real problem.
Also in the c++ version at least (i haven't looked at the java because I'm not really a java programmer) I notice you add signatures of what exceptions are thrown...
I believe these can be reduced as public api should contain this information not non public methods which should clean up signatures.
so what are your thoughts about my reply?
Because by looking at our current class it looks redundant with signatures including exception information.
> But I don't think we should introduce another exception for this, as
> it will make the signatures more bloated than they already are (in the
> Java version there are already two kinds of exceptions that must be
> propagated up, parsing exceptions and IO exceptions),
his concern is across:
public abstract boolean isMatch() throws IOException, ParsingFatalTerminalException;
protected boolean Predicate()
throws IOException, ParsingFatalTerminalException
and many more methods that look similar in both java and c++ as signatures define exceptions handled in those languages.
here is my reply:
To do's with exception:
raise specific exceptions to what the problem is.
DivideByZeroException .. there are reasons why the base class ArithmeticException is not thrown.
This has to do with making use of catch blocks and including more specific information about the exception.
A user should never parse an exception string to access exception data. (big no no) By using the correct type you have easy access to extra information you need from the exception.
However I do believe that our exceptions should inherit from the same base type: ModelException This way if the caller can expect base class ModelException and apply a catch block that will catch all child classes.
try
{
Boolean test = parser.IsMatch();
}
catch(ModelException e) {
//will catch parent and all childs that inherited form ModelException
}
//any others will be unhandled
Now if we expected other exceptions from occurring in c# and wanted to provide the caller a type to expect then the following could be done:
new SpecificExceptionName("exception message", innerException);
But this requires the caller to unravel the exception tree to access the cause of the real problem.
Also in the c++ version at least (i haven't looked at the java because I'm not really a java programmer) I notice you add signatures of what exceptions are thrown...
I believe these can be reduced as public api should contain this information not non public methods which should clean up signatures.
so what are your thoughts about my reply?
Because by looking at our current class it looks redundant with signatures including exception information.