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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:15:31+00:00 2026-05-26T00:15:31+00:00

I am not good at handling exceptions, so I need a hint at this

  • 0

I am not good at handling exceptions, so I need a hint at this occasion:
I want to put arrays in a collection (ArrayList) all of which should be of the same length. Otherwise errors emerge in computations. When an array of not desired length is to be inserted in the ArrayList, I would like to throw an exception with a message. What kind of exception is suitable for this occasion?

What worries me is that I have to check the size of the array which is to be inserted (with an if statement). Is it reasonable to have an if statement inside a try block?

Here is the relevant fragment code:

inputdata: the arrayList

arraylength: the length of the array specified by the first array inserted

Could somebody modify the try – catch block?

public void insertData(double[] arraydata){
    if(this.inputdata.isEmpty()){
        inputdata.add(arraydata);
        this.arraylength = arraydata.length; 
    }else{
        try {
           if(this.arraylength == arraydata.length)
               inputdata.add(arraydata);
        }catch(Exception exception){
            System.err.printf("Missmatch array dimensions in %d place",inputdata.size()+1);           
        }
    }
}
  • 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-26T00:15:32+00:00Added an answer on May 26, 2026 at 12:15 am

    Exceptions should only be for exceptional cases. If this is a frequent occurrence, you might want to handle it another way, with standard logic in the workflow. For example, you could retur n true if you can insert the data, and false if the array to insert is not the right length. Or you could check when the user enters the array values, and tell them then that the length has to be x.

    If this does represent an exceptional case throw an IllegalArgumentException, as in

    if(this.arraylength == arraydata.length)
        inputdata.add(arraydata);
    } else {
        throw new IllegalArgumentException("Ever array needs same length...");
    }
    

    something like that.

    As written, your code right now is catching any exception thrown in the add operation. You should throw, instead of catch, an exception in your insertData method, as my example demonstrates. The exception should be caught outside of the insert data method. This means you don’t need a try/catch statement in insertData.

    Also note that IllegalArgumentException is a Runtime exception, so it does not need to be thrown or caught if you don’t want to. You still can catch it, if you want.

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

Sidebar

Related Questions

I'm not good at t-sql, so I need help. I have this code I
Is there any good practice related to dynamic_cast error handling (except not using it
Im not good at math, I could find everything so far, but not this
I'm not good with JavaScript, and this is Google's code. I'm getting a object
I'm not good in communicating between controllers, so i need to setup and check
This question is more aimed at good design practices rather than exceptions and stacks.
in product mode in GWT the assertion is not available which is good, but
It seems not good for me, reporting errors like 'General Error: '+...., Is there
I am not good at SQL Server 2000. I have a comma-delimited list of
I am not good at jsp but I wondered what can cause such a

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.