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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:57:33+00:00 2026-05-26T20:57:33+00:00

Somehow i came across a bug today and i was trying to replicate it.

  • 0

Somehow i came across a bug today and i was trying to replicate it.

I believe the below code will throw a NullPointerException because ep is not instantiated, but i am unable to simulate it..

I believe that the instance variable ep is null when the exception is thrown from the main. But running it doesn’t show NullPointerException.

When the exception is thrown (throw e.getILException(100)), is the MyException.MyException() constructor called?

package sg.java.testException;

public class MyException extends Exception {

    Exception ep;

    public MyException() {
        super();
        ep = new Exception();
    }

    public OtherException getILException(long txnId) {
            // the error is here, according to the stacktrace
        return new OtherException(ep, ep.toString());
    }
}

Caller:

public static void main(String args[]) throws OtherException{
        try{
            Car.run();
        }catch (MyException e){
            throw e.getILException(100);
        }
    }

Car Codes:

public class Car {
    public static void run() throws MyException {
        throw new MyException();
    }
}

I want to be able to replicate this exception:

java.lang.NullPointerException
    at sg.java.testException.MyException.getILException(MyException.java:13)
    at sg.java.testException.ExceptionTest.main(ExceptionTest.java:8)

Thanks!

  • 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-26T20:57:34+00:00Added an answer on May 26, 2026 at 8:57 pm

    When the exception is thrown (throw e.getILException(100)), is the MyException.MyException() constructor called?

    No. It is called before that. The statement throw e.getILException(100) invokes the method getILException(...) then throws whatever returned from the method.

    I comment out the line ep = new Exception(); in MyException‘s constructor.

    class OtherException extends Exception {
        OtherException(Exception ex, String exStr) {}
    }
    
    class MyException extends Exception {
    
        Exception ep;
    
        public MyException() {
            super();
            // ep = new Exception();
        }
    
        public OtherException getILException(long txnId) {
                // the error is here, according to the stacktrace
            return new OtherException(ep, ep.toString());
        }
    }
    
    public class Test {
        public static void main(String args[]) throws OtherException {
            try{
                throw new MyException();
            }catch (MyException e){
                throw e.getILException(100);
            }
        }
    }
    

    The following is the result.

    Exception in thread "main" java.lang.NullPointerException
    at MyException.getILException(Test.java:16)
    at Test.main(Test.java:25)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been going through some code and came across a statement that somehow
Today, during some other somehow related work, came across my mind the following question:
I have been trying to play around with gedit and came across the shortcut
In the code I am currently working on I came across a piece code
i've been reading some lisp code and came across this section, didn't quite understand
I came across a frustrating something in Haskell today. Here's what happened: I wrote
I came across the following code with replaces one variable in a language file
The Problem It's something I came across a while back and was able to
I was reading the Python docs about classes and came across this paragraph which
While reading some documentation here , I came across this: unsigned unitFlags = NSYearCalendarUnit

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.