Java: exceptions and 'might not have been initialized'
<p>I am very new to Java.  I am writing a program to read a file, compute its SHA1 checksum, and write the result to another file.  On any error I am calling a function <code>err_exit()</code> that prints a message to stderr and terminates execution by calling <code>System.exit()</code> with a specified exit status.  This is approximately what my <code>main()</code> function looks like:</p>

<pre><code>public static void main(String[] args) {
    String in_fname = "C:/tmp/test.txt"; // not reading args yet
    String out_fname = "C:/tmp/test.sign";
    byte[] file_data;
    String hexdigest;

    try {
        file_data = readFileAsByteArray(in_fname);
    }
    catch (java.io.IOException ex) {
        file_data = new byte[] {0};  // note this line well, please
        err_exit(2, "error opening input file '" + in_fname + "'");
    }

    try {
        hexdigest = hexdigestSha1(file_data);
    }
    catch (NoSuchAlgorithmException ex) {
        hexdigest = "";  // note this line well, please
        err_exit(3, "could not compute SHA1 message digest!");
    }

    try {
        writeFileFromString(out_fname, hexdigest);
    }
    catch (java.io.IOException ex) {
        err_exit(2, "error writing output file '" + out_fname + "'");
    }

    System.exit(0); // success
}
</code></pre>

<p>There are two lines that I asked you to note well.  Both of those lines exist simply to keep the compiler from complaining that the variable might not be initialized.</p>

<p>As far as the compiler can tell, the catch block might continue.  Actually <code>err_exit()</code> will never return so there is no chance of an invalid value being passed along.</p>

<p>So, my question: what is the usual Java idiom for handling this sort of thing?  The row of <code>try</code>/<code>catch</code> blocks is sort of ugly; would you recommend I make the various functions call <code>err_exit()</code> and not have the code explicit like this?  I think I prefer the explicit checks, and the <code>main()</code> function is the right place to do the checking, but I'm interested in feedback.</p>

<p>If I am going to have the <code>try</code>/<code>catch</code> blocks, is this a good way to silence the compiler warnings?</p>

<p>If I were doing this in Python, I'd likely just not catch the exceptions, and let the program stop with a stack trace.  A stack trace on error won't shock the user of this program, since that user is me.  It occurs to me that if I declared my <code>main()</code> function as <code>throws Exception</code> that I could then not catch the exceptions and it would behave something like Python.  Is that a horrible idea that would make right-thinking Java people shun me?</p>

<p>P.S. If you have a favorite book/web page/whatever of Java idioms that I should read, please mention it.</p>

<p>EDIT: I apologize for the variable names with underscores.  I have already renamed the ones in my real program, but I am going to leave them as-is here.  It's actually because I have spent a lot of time programming in Python and C; I was either using Python "PEP 8" style or generic C style, take your pick.</p>
