-4

May I generalize that any operation with a file may cause IOException (IOE)? Including just reading a file, just checking its attributes (name, timestamp)?

I am trying to find logic in API methods throwing IOException, so I try to understand and make a general rule rather to memorize.

FileInputStream / FileOutputStream constructor don't throw IOException (they throw its child FileNotFoundException thou) - but this is not working with file yet, it is just preparing for it - another method would access a file given FileInputStream / FileOutputStream as argument.

  1. So far my generalization holds everywhere. Any cases this generalization is wrong (exceptions or wrong in principle)?
  2. Is there any other generalization concerning checked Exceptions (maybe IOE concerning sockets or any other checked exception besides IOE)?

May be there are some OS internals that help generalize it understanding some general ideas happening on OS Level?

user10777718
  • 723
  • 4
  • 16
  • 2
    Don't try to generalize or memorize. Read the docs, and it never hurts to refer to the documentation while you're writing code. A good IDE will help you do that. – markspace Jan 06 '19 at 08:19
  • There are many different OSes Java is running on. Worst case you get exception on OS A for something that works nicely for B. – GhostCat Jan 06 '19 at 08:30

1 Answers1

4

AFAIK, there is no general rule for deciding if IOException or a subclass of IOException is thrown. (And I wouldn't fill my head by memorizing that kind of information anyway. I would just read the javadocs as required.)

If you want to analyze this for yourself, the Java source code can be downloaded from the OpenJDK.java.net site. It should be possible to "grep" the source code tree for all occurrences of the respective exceptions.

May be there are some OS internals that help generalize it understanding some general ideas happening on OS Level?

The OS doesn't throw Java exceptions. The Java class libraries and Java native libraries do.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216