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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:29:48+00:00 2026-06-15T00:29:48+00:00

Im kinda new to handling exceptions in Java with Junit, a little guidence would

  • 0

Im kinda new to handling exceptions in Java with Junit, a little guidence would be much appreciated.

What I am trying to do:

  • I surround the creation of the new CustomObject with a try as the user can pass in a String that will not match an enum when we call valueof(). I want to be able to catch an exception here, which I am, though I am told: “A catch statement that catches an exception only to rethrow it should be avoided.”. There must be a better way to handle this?

  • If the new object has the correct enum then I call isValidObject, which returns a boolean. If the Integer is not valid then I throw an exception.

  • My test has a @Test(expected = AssertionError.class) and is passing.

Is there a better/cleaner way to use the exceptions?

I have the code below:

private CustomObject getObjectFromString(String objectDataString) {
        if (objectDataString != null) {
            String[] customObjectComponents = objectDataString.split(":");
            try {
                CustomObject singleObject = new CustomObject(EnumObjectType.valueOf(customObjectComponents [0]),
                        Integer.parseInt(customObjectComponents [1]));
                if (isValidCustomObject(singleObject)) {
                    return singleObject;
                } else {
                    throw new IllegalArgumentException("Unknown custom object type/value: " + EnumObjectType.valueOf(customObjectComponents [0]) + ":"
                            + Integer.parseInt(customObjectComponents [1]));
                }
            } catch (IllegalArgumentException e) {
                throw e;
            }

        }

Oh, and if anyone can recommend anything good to read about exception handling, that would be great.

  • 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-15T00:29:49+00:00Added an answer on June 15, 2026 at 12:29 am

    A catch statement that catches an exception only to rethrow it should be avoided.”. There must be a better way to handle this?

    Yes, simply remove the try catch. Your code is equivalent to:

    private CustomObject getObjectFromString(String objectDataString) {
        if (objectDataString != null) {
            String[] customObjectComponents = objectDataString.split(":");
            CustomObject singleObject = new CustomObject(EnumObjectType.valueOf(customObjectComponents[0]),
                    Integer.parseInt(customObjectComponents[1]));
            if (isValidCustomObject(singleObject)) {
                return singleObject;
            } else {
                throw new IllegalArgumentException("Unknown custom object type/value: " + EnumObjectType.valueOf(customObjectComponents[0]) + ":"
                        + Integer.parseInt(customObjectComponents[1]));
            }
        }
    }
    

    That code will throw an IllegalArgumentException if the value passed to your enum.valueOf() is not valid or if your isValidCustomObject method returns false.

    Note that it might also throw an IndexOutOfBoundException if the string does not contain a : which you probably want to test before calling customObjectComponents[1]. And it might throw NumberFormatException too.

    And you seem to accept a null String as a valid entry, which is probably not a good idea (depends on your use case obviously).

    I would probably have written it that way:

    private CustomObject getObjectFromString(String objectDataString) {
        Objects.requireNonNull(objectDataString, "objectDataString should not be null");
        String[] customObjectComponents = objectDataString.split(":");
        if (customObjectComponents.length != 2) {
            throw new IllegalArgumentException("Malformed string: " + objectDataString);
        }
    
        EnumObjectType type = EnumObjectType.valueOf(customObjectComponents[0]);
        try {
            int value = Integer.parseInt(customObjectComponents[1]);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(customObjectComponents[1] + " is not an integer);
        }
    
        CustomObject singleObject = new CustomObject(type, value);
        if (isValidCustomObject(singleObject)) {
            return singleObject;
        } else {
            throw new IllegalArgumentException("Unknown custom object type/value: " + type + ":" + value);
        }
    }
    

    And finally, it would probably make sense for the CustomObject’s constructor to check whether its arguments are ok or not by itself, instead of having to call a separate isValid method. The last block would then simply be:

        return new CustomObject(type, value);
    

    which would throw an IllegalArgumentException from the constructor if required.

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

Sidebar

Related Questions

I'm kinda new to Java, so haven't yet fully grasped the concept of multithreading.I
I'm kinda new to Flex. I have trying to send Hash from Ruby on
I'm new here and kinda new to java. I've encountered a problem. I have
I'm trying to create a custom webpart. To implement error handling I would like
Yesterday I red this article about the new Exception Handling in Java 7. In
I am kinda new to handling images, there are many things that I do
Kinda new to iOS, Im also studying Android. kinda confuse on arrays. How to
Im kinda new to drupal, and its views. SO can anybody tell me how
I'm kinda new to javascript running on Apache but I have the following problem.
I am kinda new to this Regex thing. When analyzing some code I frequently

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.