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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:10:21+00:00 2026-05-15T03:10:21+00:00

Is there any specific protocol for handling exceptions in public methods? Consider this eg.

  • 0

Is there any specific protocol for handling exceptions in public methods? Consider this eg.

public int someMethod()
{
  try{
  code that might throw an exception    
  }
  catch(Exception e)
  {
     log the exception
  }
}

Say that this method might throw an ArrayIndexOutOfBoundsException. So, is it correct to handle this Exception in the method itself (as in the example) or throw it and assume that the calling method will handle the Exception?

Edit:
Further extending my question.
Consider the following function.

public int[] someMethod2()
{
  try{
code that might throw an exception    
 }
catch(Exception e) {
  log the exception
  return new int[0];
 }
}

As in the example code above, if I return an array of size 0, then the calling method will fail with an AraryIndexOutOfBounds Exception. If I return null, then the calling method will fail with NullPointer Exception. Since, I can modify the calling method, which way is better? Should I let the calling method fail? Or should I directly call System.exit() in someMethod2()?

Is there a tutorial which explains these decisions? This does not give me an answer.

  • 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-15T03:10:21+00:00Added an answer on May 15, 2026 at 3:10 am

    Generally, don’t handle the exception if it doesn’t make sense to handle it yourself. ArrayIndexOutOfBoundsException usually is a sign of a programming error, and if your app contains programming errors, crashing is the correct and responsible thing to do.

    One big no-no is returning null instead of throwing exceptions, like this code:

    public Thing getThingById(long id) {
        try {
            ResultSet rs = queryDatabase("SELECT * FROM THING WHERE ID = " + id);
            rs.next();
            return new Thing(rs.get(1), rs.get(2), rs.get(3));
        } catch (Exception e) {
            return null;
        }
    }
    

    Now, if you get null from this method, you don’t know why. Was the ID invalid? Is the database server down? Or was it some programming error in the queryDatabase method?

    Edit: Oh, and you should never log and rethrow, it’ll just fill up your logs and make them unreadable. If you rethrow an exception, odds are that whatever code catches it further up the call chain will log it, too

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

Sidebar

Ask A Question

Stats

  • Questions 438k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Finally found the error, I feel dumb, it was a… May 15, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer Have a look at the UIView animation methods i.e. -… May 15, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer Heh, ended up going with the MooTools one. May 15, 2026 at 4:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.