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 7705211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:49:22+00:00 2026-05-31T23:49:22+00:00

Just a quick question that I can’t seem to find a proper answer to

  • 0

Just a quick question that I can’t seem to find a proper answer to online.

When I have a try/catch block, and I want to catch an error, should I use System.err.println(...) or should I use throw new Exception(...).

For example:

if (null==userList || null==credentials)
    System.err.println("Did you call login() first?");
else
{
    try
    {
        currentUser=(String) userList.nextElement();
        currentPass=(String) credentials.get(currentUser);          
    }
    catch(NoSuchElementException nsee)
    {
        System.err.println("Is properties file blank?");
        //Or should this be
        throw new NoSuchElementException("Is properties file blank?");
        nsee.printStackTrace();
    }
}

EDIT: So is throw not meant to go inside the catch? Can I use throw instead of catch or is it just for method signatures??
What is the correct method of catching an error in this case, where I’ll be trying to ensure the properties file is not blank and contains values?
Also, for clarity, my goal is to make it as clear as possible to the user what the error is, so they know straight away that they need to fix their properties file.

  • 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-31T23:49:22+00:00Added an answer on May 31, 2026 at 11:49 pm

    Depends on what you want to do.

    If the contract of your method can’t be fulfilled in case of an exception (i.e. you can’t return the value you intended to for instance) throw a new exception. (Note however that just rethrowing a new exception doesn’t make much sense. Just declare the method as throws NoSuchELementException and let the exception propagate.)

    If your contract can be fulfilled in case of an exception (i.e. you could return null if no element is found) then you should catch the exception (log it using System.err.println if you like) and then continue execution.


    A side-note on the first option of throwing a new exception:

    You should always throw an exception which is appropriate for the current level of abstraction. If for instance the method is called getUserFromList(...) and you encounter a NoSuchElementException due to some collection-related problem, you should catch the exception and throw a NoSuchUserException.

    To highlight the difference:

    try {
        currentUser=(String) userList.nextElement();
        currentPass=(String) credentials.get(currentUser);          
    
    } catch(NoSuchElementException nsee) {
                  ^^^^^^^
    
        System.err.println("Is properties file blank?");
        throw new NoSuchUserException("Is properties file blank?");
                        ^^^^
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick simple question that I can not seem to find an answer
A quick question, that I can't seem to find the answer to anywhere to
just a question that needs a quick answer, I have a Action, lets say,
Just a quick question that no doubt someone out there will know the answer
I've just got a quick question that if you have time would be great
Ok guys, just a quick question hopefully someone can find my mistake quickly, but
Just a quick question. I understand that Singleton patterns can be extended and that
I have a quick question that I can't figure out. I've tried searching Google
just a quick question. First of all, let me verify that I have the
Just a quick question: Do you know why i can do that: pnrCode =

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.