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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:06:22+00:00 2026-06-14T06:06:22+00:00

Below is the class somebody else wrote. The problem that I am facing is

  • 0

Below is the class somebody else wrote.

The problem that I am facing is that when it get’s into the parse method with null as the rawString, it is throwing NumberFormatException.

So what I was thinking to do is, I should catch that NumberFormatException and set the value itself as null. So the way I did is right?

public class ByteAttr {

    @JExType(sequence = 1)
    private Byte value;

    public static ByteAttr parse(String rawString) {
        ByteAttr attr = new ByteAttr();
        try {
            attr.setValue(Byte.valueOf(rawString));
        } catch (NumberFormatException nfEx) {
            attr.setValue(null);
        }
        return attr;
    }

    public Byte getValue() {
        return this.value;
    }

    public void setValue(Byte value) {
        this.value = value;
    }
}
  • 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-14T06:06:23+00:00Added an answer on June 14, 2026 at 6:06 am

    The correct approach depends on what you want to accomplish in the program.

    • If it makes sense for ByteAttr.getValue() to return null later in your program, then your approach could work.
    • However, you need to consider whether you should be throwing an exception if parse is called with an indecipherable argument (including null). An alternative is to catch the NumberFormatException and throw a different exception that has semantic meaning in your program.
        public static ByteAttr parse(String rawString) throws BadAttributeException {
            ByteAttr attr = new ByteAttr();
            try {
                attr.setValue(Byte.valueOf(rawString));
            } catch (NumberFormatException nfEx) {
                throw new BadAttributeException(nfEx); // wrap original exception
            }
            return attr;
        }
    
    • Another technique is to pass a default value to parse for those cases when rawString is indecipherable:
        public static ByteAttr parse(String rawString, Byte defaultValue) {
            ByteAttr attr = new ByteAttr();
            try {
                attr.setValue(Byte.valueOf(rawString));
            } catch (NumberFormatException nfEx) {
                attr.setValue(default);
            }
            return attr;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I'm using a class for custom messageboxes. But my problem is that, icon background
Scala's Option class has an orNull method, whose signature is shown below. orNull [A1
I am facing problem with hibernate annotations.For the code shown below I have a
I have a problem with my class below: Iam tring to hold UserID without
I am instantiating a class A (which I am importing from somebody else, so
Does making the below class final (adding public final class) have any real impact
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
When I try to execute the below class using ICriteria, if (_userGroupId > 0

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.