Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7690021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:18:06+00:00 2026-05-31T20:18:06+00:00

I am very new to Java. I am writing a program to read a

  • 0

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 err_exit() that prints a message to stderr and terminates execution by calling System.exit() with a specified exit status. This is approximately what my main() function looks like:

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
}

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.

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

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

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

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 main() function as throws Exception 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.S. If you have a favorite book/web page/whatever of Java idioms that I should read, please mention it.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:18:07+00:00Added an answer on May 31, 2026 at 8:18 pm

    You seem to have a strong C background. I assume the compiler complains about variables such as file_data. Simply add file_data = null above the try-catch block.

    Some points on the above:

    1. I think it is safe to combine all the statements in a single block. When an exception is thrown, the program will branch to the correct exception block immediately. It will also look far less ugly.
    2. Usually another method is not called within the catch-block as the task should (usually and if possible) be short and to the point. Handle the error, continue or exit (possibly be re-throwing the exception).
    3. System.exit() is evil. Rather throw a wrapped RuntimeException if you want the system to exit (i.e. an unrecoverable error condition). System.exit() may result in non-clean exists. Rather use throw new RuntimeException(e);
    4. Lastly, and if I may be as blunt, the Java convention is to write method/variable names as errExit, rather than err_exit.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really new to Java and I read that synchronized is very expensive
I am writing a java program that needs a file open dialog. The file
Im very new to java (mainly duel with embedded only) Im writing a simple
I'm new too Java, but I'm very experienced with PHP. When I was writing
I am very new to Java, I want to add a PNG image to
I'm very new to Java but I've been developing a habit to use final
I am very new to processing.org and Java. I am trying to store objects
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I'm soon going to check in the very first commit of a new Java
Im very new to SQL but need to write a query to do the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.