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

  • Home
  • SEARCH
  • 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 8912439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:11:51+00:00 2026-06-15T04:11:51+00:00

I have built this class up from various post as good as i could.

  • 0

I have built this class up from various post as good as i could.

I am trying to get a list of usernames from a MySQL database.

Here is the method retrieving them:

public ArrayList<String> LoadUsers() throws SQLException {
    ArrayList<String> players = new ArrayList<String>();
    try {
        Connection conn = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            Statement s = conn.createStatement ();
            s.executeQuery ("SELECT * FROM NFT_users");
            rs = s.getResultSet ();

            while(rs.next ()){
                players.add(rs.getString("name"));
            }
            rs.close ();
            s.close ();
        }

        catch (SQLException ex) { 
            System.out.println(ex.toString()); 
        }

        finally { 
            try { 
                if (conn != null) conn.close(); 
            } 

            catch (SQLException ex) { 
                System.out.println("On close: " + ex.toString()); 
            } 
        }
    } 

    catch(Exception ex) { 
        //trace(ex); 
    }

    return players;
}

And this is the code in my mainclass which retrieves it from the method:

    ArrayList<String> players = database.LoadUsers();

However, i get the error Must be caught or declared to be thrown. What do i do wrong?

  • 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-06-15T04:11:52+00:00Added an answer on June 15, 2026 at 4:11 am

    Your method: –

    public ArrayList<String> LoadUsers() throws SQLException {
    

    has declared an SQLException in it’s throws clause. So, from whichever method you call this method from, you would either need to enclose the invocation in a try-catch block to handle this exception, or declare this exception in the throws clause of that method also.

    So, suppose you are calling your method from a method caller().

    So, you have two options: –

    1. Declare exception in throws clause of caller: –

      public void caller() throws SQLException {
           ArrayList<String> players = database.LoadUsers();
      }
      
    2. Enclose the method invocation in a try-catch. In this case, remember to declare your list outside the block first:

      public void caller() {
           ArrayList<String> players = null;
           try {
                players = database.LoadUsers();
           } catch (SQLException e) {
                e.printStackTrace();
           }
      }
      

    Note that, if you are using the first option, then you will again face same problem in the method that called caller. There also you would have to follow this thing.

    In general, an exception raised inside a method is – either handled there using try-catch block, or is propagated up the stack trace to the immediate caller method, but not both. You are doing both in your method. You are handling the exception, and have declared it to be thrown in the throws clause too. You should never do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows service built upon ATL 7's CAtlServiceModuleT class. This service serves
I have a page with an unordered list built like this: <ul id='shows'> <div
I could equally have titled this question, Is it good enough for CRAN? I
I have build a c# class library verification.dll using OpenCVSharp. This references OpenCvSharp.dll in
I have built this accordion using the jquery ui accordion plugin, I sort of
I have built a T-SQL query like this: DECLARE @search nvarchar(1000) = 'FORMSOF(INFLECTIONAL,hills) AND
I know this sounds confusing: I have just built some basic prev/next pagination for
I have a multi-step order form built in this manner: Step 1: Choose category
I've never done this before. The website I have just built is the second
I have an HTML table containing a few rows (this is built dynamically). All

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.