Note that there are some explanatory texts on larger screens.

plurals
  1. PONew to Java : Constructor error control
    primarykey
    data
    text
    <p>I'm new to Java and I'm porting one of my C++ libraries to Java as a learning experiment. This is not homework (as should be obvious from looking at my code). I have a few questions concerning the following code of my constructor for an ESRI shape file reader.</p> <pre><code>import java.io.*; /** * * @author Bill */ public class ShapeFileReader { public FileInputStream inStream; /* * @param fileName File name string. Must not be null or zero length. * @throws Exception if file specified by fileName fails to open */ public ShapeFileReader(String fileName) throws IOException { if(fileName == null) throw new NullPointerException("fileName was null"); if(fileName.length() == 0) throw new IllegalArgumentException("fileName string length was zero"); File fi = new File(fileName); if(fi.exists() == false) throw new IOException("File does not exist: " + fileName); // Catch-or-specify (this method already throws IOException) inStream = new FileInputStream(fileName); } } </code></pre> <p>During parameter validation and existence should I be throwing the exceptions as shown? The validation throws unchecked exceptions, and the existence throws checked exceptions. I assume that <code>FileInputStream</code> constructor will also throw an <code>IOException</code>, but I specified that in the method <code>throws</code> clause. </p> <p>I was considering refactoring the opening of the file to a seperate function, but I figured it would be more useful and simple to do this in the constructor, and also learns me how to control errors here. Besides, any instance of this object will not have a closed/open state. All of these objects are reserved strictly for READING a file only, so they are created on a as-needed basis per file. I will provide a <code>close()</code> method seperately.</p> <p>Also, from an extensibility point of view, would it be difficult to adapt to reading a file over a network using the <code>FileInputStream</code> with the current constructor? Or should I use a different class and multiple constructors?</p> <p>Thanks for any and all input.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload