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

  • Home
  • SEARCH
  • 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 5982673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:07:14+00:00 2026-05-22T22:07:14+00:00

My Java application needs to serialize/deserialize an XML structure received via HTTP. This XML

  • 0

My Java application needs to serialize/deserialize an XML structure received via HTTP. This XML message may contain an Error element on almost any level of the XML. That’s why all classes extend ApiError. Please see the following question as well: Deserialize repeating XML elements in Simple 2.5.3 (Java)

I have created the following class:

public class ApiError {
  private String code;
  private String level;
  private String text;

  public get/set...() {}

}

Almost every other class in my application extends the ApiError class as those classes may raise an error.

I’d like to have a method like getErrorOrigin() which returns the name of the class which first created an instance of ApiError?

Is there an easy way in Java how to do this?

Thanks,
Robert

  • 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-22T22:07:14+00:00Added an answer on May 22, 2026 at 10:07 pm

    Yes, you can do this, by getting the stack trace of the current thread in the ApiError constructor:

    public ApiError() {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        this.errorOrigin = trace[2].getClassName();
    }
    

    You can see why this works by putting

    Thread.dumpStack();
    

    in the ApiError constructor. If you create a new instance of ApiError, you’ll see a stack trace that looks like this:

    java.lang.Exception: Stack trace
            at java.lang.Thread.dumpStack(Thread.java:1249)
            at ApiError.<init>(Test.java:39)
            at Test.main(Test.java:9)
    

    Thread.currentThread().getStackTrace() will give you an array of StackTraceElement objects:

    • The first element of that array will be for the getStackTrace() method itself, which is executing when the trace is generated.
    • The 2nd element will be the ApiError constructor, which called getStackTrace().
    • The 3rd element – the one you want (hence the index 2) – is for the method that called the ApiError constructor. By calling getClassName() on that element, you get the name of the class containing the method that invoked the ApiError constructor.

    Having said all that, it seems you’re trying to reimplement exceptions. I would seriously consider throwing exceptions from your methods, rather than returning ApiError objects. This keeps the error handling separate from your ‘business logic’. Also errors are (hopefully) an exceptional situation, so it makes sense to use exceptions for error handling, rather than having ApiError pollute your class hierarchy.

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

Sidebar

Related Questions

I have a java application that needs to display currency symbols. I'm running on
I'm developping a Java application . I created this interface with MockupScreens . Please
I am developing a social application on top of Java and Cassandra database. I
My application needs to save large images in a lossless format. I'm willing to
My application needs to talk to more than one secure servers. I am using
I'm working on a client-side Java application for which I want to create a
I'm trying to use the SQLite4Java library in my java application, when I try
What is the best practice for providing Help Contents in a Java Application. Should
They say premature optimisation is the root of all evil, but here goes... We
I am trying to use Protocol buffers to store serialized data in Database for

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.