2

I notice that methods in some sophisticated libraries, including the standard JDK, tend to throws upcasting exception. At first I found this phenomenon in the source code of Apache POI, later see it again in java.io.FileWriter like this:

public FileWriter(String fileName) throws IOException {
    super(new FileOutputStream(fileName));
}

where the instantiation of FileOutputStream declares the only checked exception FileNotFoundException in the this callstack. However, IOException is declared, which is the super class of FileNotFoundException.

So what is the reason for this? Or does it simply depend on the habit of programmer?

JRG
  • 4,037
  • 3
  • 23
  • 34
SedriX
  • 500
  • 3
  • 10
  • 1
    What do you mean by this? "However, IOException is thrown as its upcasting super class." That sentence is not at all clear to me. – Jon Skeet Jul 31 '17 at 08:31
  • 1
    What OP is asking is why `FileWriter` throws a more general exception than its only call to `FileOutputStream` would do. – luk2302 Jul 31 '17 at 08:32
  • @luk2302: I suspect that's the case, but it would certainly help if the OP could clarify. (There isn't any casting here... there's just the choice to declare a more general checked exception than would strictly be required by the code. The talk about casting makes this question confusing.) – Jon Skeet Jul 31 '17 at 08:32
  • See also https://stackoverflow.com/questions/22610768/java-exception-handling-concept-why-do-compiler-allow-to-write-exceptions-in-th – Raedwald Jul 31 '17 at 08:36
  • Historical note: in jdk 1.1, `FileOutputStream(String)` was declared as throwing `IOException`. Therefore `FileWriter(String)`, which used that constructor, also had to be declared as throwing `IOException`. Subsequently, `FileOutputStream` (but not `FileWriter`) was changed to throw a more specific `FileNotFoundException`, but the Javadoc was then changed to read "throws FileNotFoundException if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason." It seems to me that this is a misleading exception. – Klitos Kyriacou Jul 31 '17 at 09:49

1 Answers1

5

It may make sense to avoid changing the API too frequently.
Today, a method can throw in the code a subclass of IOexception but tomorrow it could throw another one.

While the parent exception declared in the signature is not too general and doesn't loose value for the clients, it seems ok to declare a base class for the exception.
For example, a bad use would be to declare throw Exception as the client could not understand the general meaning of the exception and handle it consequently.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 1
    Congrats. You are a lucky person today. I was just about to finish my answer when the question got closed out. – GhostCat Jul 31 '17 at 08:45
  • @GhostCat Sorry for that GhostCat, I still remember the first time when I posted here to ask something about java and received your criticism :) (and that's what promotes me). Looking forward to meet you in more questions! – SedriX Jul 31 '17 at 10:01
  • You are very welcome. And when no upvotes come in today, so at least some go out. – GhostCat Jul 31 '17 at 10:22