Moderators: phlip, Larson, Moderators General, Prelates
public void read() throws IOException {
is.read();
}
public void read() throws IOException {
try{
is.read();
} catch (IOException e) {
throw e; // Or new IOException() or whatever
}
}
log is a File:
mode is append,
path is "./log.txt",
when permission denied:
throw permission denied,
when could not create:
die "cannot log actions",
public void loadImage(String filename) throws imglib.FileNotFoundException
{
try
{
inputStream = new FileInputStream(filename);
}
catch (java.io.FileNotFoundException e)
{
throw new imglib.FileNotFoundException(e);
}
}phlip wrote:A concrete example: A is some program that has to process images. B is an image-processing library. X is a file-handling library.
A needs to load an image, but the file doesn't exist. It says "load this image" to B, and then B says "open this file" to X. X throws a file-not-found exception. It is entirely reasonable for B to allow that file-not-found exception to propagate up to A. A is the place where handling for that exception can reasonably be placed. And it certainly doesn't break the encapsulation of B to do so - the expected result of calling "load this image" with an invalid filename is a file-not-found exception.
A is some program that has to process images. B is an image-processing library. X is a file-handling library.
A needs to load an image, but the file doesn't exist. It says "load this image" to B, and then B says "open this file" to X. X throws a file-not-found exception
Jplus wrote:The primary difference between an exception throw and a return statement is that an exception can unwind multiple levels of the call stack at once. If you don't want that to be possible, perhaps for aesthetical reasons (which I can understand), you don't need exceptions.
No. You'd know that if you'd read my post more carefully:shawnhcorey wrote:@Jplus: You're thinking that A, B, and X are all written by the same programmer at the same time.
Jplus wrote:If B doesn't catch that exception, that automatically means that B may also throw an IOException, which makes it part of B's interface as well. This is something that should be documented, which sloppy library developers may fail to do when working with a language that doesn't enforce it, but technically this is how it is.
This clearly demonstrates that you're talking about a documentation problem, as I already suggested.shawnhcorey wrote:What if X is written first, a year later B, and two years after that A, all by different programmers. For A to correctly handle X's exceptions, the programmer would have to know about B's use of X. In other words, when using a class, read all the code (because the documentation may be incomplete) for all the classes you use, all the classes they use, and so on and on and on and on. On the other hand, if exceptions are only thrown back to the calling object, all you would have to know to successfully handle all exceptions is to know the API for only the classes you use.
Note that you're almost contradicting yourself here. Either you want to force programmers to add code for handling errors, or you give functions an explicit exception interface and you want code to fail when an exception is forwarded (not caught) while it isn't part of the interface. In the first case you should simply leave exceptions out of your language and force errors to be reported through discriminated unions, as I said before. In the second case you should use mandatory exception specifications, as letterX explained.shawnhcorey wrote:I think exceptions should always be used since programmers are lazy; they don't always check a return status to see if it's successful. With exceptions, if the programmer wants the program to continue pass the exception, he must add the code to handle it. Otherwise the program stops with error.
Jplus wrote:Exceptions add nothing to a language unless you allow them to unwind more than one level on the call stack.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
Users browsing this forum: No registered users and 2 guests