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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:29:14+00:00 2026-05-14T00:29:14+00:00

I am writing a parser for csv-files, and sometimes I get NumberFormatException. Is there

  • 0

I am writing a parser for csv-files, and sometimes I get NumberFormatException. Is there an easy way to print the argument value that caused the exception?

For the moment do I have many try-catch blocks that look like this:

String ean;
String price;

try {
    builder.ean(Long.parseLong(ean));
} catch (NumberFormatException e) {
    System.out.println("EAN: " + ean);
    e.printStackTrace();
}

try {
    builder.price(new BigDecimal(price));
} catch (NumberFormatException e) {
    System.out.println("Price: " + price);
    e.printStackTrace();
}

I would like to be able to write something like:

try {
    builder.ean(Long.parseLong(ean));
} catch (NumberFormatException e) {
    e.printMethod();    // Long.parseLong()
    e.printArgument();  // should print the string ean "99013241.23"
    e.printStackTrace();
}

Is there any way that I at least can improve my code? And do this kind of printing/logging more programmatically?

UPDATE: I tried to implement what Joachim Sauer answered, but I don’t know if I got everything right or if I could improve it. Please give me some feedback. Here is my code:

public class TrackException extends NumberFormatException {
    private final String arg;
    private final String method;

    public TrackException (String arg, String method) {
        this.arg = arg;
        this.method = method;
    }

    public void printArg() {
        System.err.println("Argument: " + arg);
    }

    public void printMethod() {
        System.err.println("Method: " + method);
    }
}

The Wrapper class:

import java.math.BigDecimal;
public class TrackEx {
    public static Long parseLong(String arg) throws TrackException {
        try {
            return Long.parseLong(arg);
        } catch (NumberFormatException e) {
            throw new TrackException(arg, "Long.parseLong");
        }
    }

    public static BigDecimal createBigDecimal(String arg) throws TrackException {
        try {
            return new BigDecimal(arg);
        } catch (NumberFormatException e) {
            throw new TrackException(arg, "BigDecimal.<init>");
        }
    }
}

Example of use:

try {
    builder.ean(TrackEx.createBigDecimal(ean));
    builder.price(TrackEx.createBigDecimal(price));
} catch (TrackException e) {
    e.printArg();
    e.printMethod();
}

EDIT: Same question but for .NET: In a .net Exception how to get a stacktrace with argument values

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

    You can easily implement such detailed information on custom-written exceptions, but most existing exceptions don’t provide much more than a detail message and a causing exception.

    For example you could wrap all your number parsing needs into a utility class that catches the NumberFormatException and throws a custom exception instead (possibly extending NumberFormatException).

    An example where the some additional information is carried via the exception is SQLException which has
    a getErrorCode() and a getSQLState() method.

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

Sidebar

Related Questions

I'm writing some java code that's supposed to parse csv files with different column
I'm writing a parser for some LISP files. I'm trying to get rid of
I am writing a string parser and the thought occurred to me that there
I'm writing a simple app that processes POST ed CSV files and am testing
Currently i have a parser setup that will parse through csv files of ~2
I'm writing a parser for CSV data, and am trying to determine how to
I am writing an iPad app that downloads a rather large .csv file and
I am writing a parser to parse incoming text files. I have it to
I am currently writing a parser for html file generated from doc files. The
I'm writing a CSV file. I need to write timestamps that are accurate at

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.