What does <init> signify in a Java exception?

10,715

Solution 1

That the exception is thrown in the construction of the object, there are two options:

  • in the constructor
  • while initializing variables

Check out this demo I wrote: http://ideone.com/Mm5w5


class Main
{
        public static void main (String[] args) throws java.lang.Exception
        {
                try
                { new Test(); } catch (Exception e) { e.printStackTrace(); }

                try
                { new Test2(); } catch (Exception e) { e.printStackTrace(); }

                try
                { new Test3(); } catch (Exception e) { e.printStackTrace(); }


        }

        static class Test
        {
                Object obj = getObject();
                Object getObject()
                { throw new RuntimeException("getObject"); }
        }

        static class Test2
        {
                Test2()
                {
                        throw new RuntimeException("constructor");
                }
        }

        static class Test3
        {
                Object obj1 = null;
                String str = obj1.toString();
        }
}

Produces:

java.lang.RuntimeException: getObject
    at Main$Test.getObject(Main.java:24)
    at Main$Test.<init>(Main.java:22)
    at Main.main(Main.java:9)
java.lang.RuntimeException: constructor
    at Main$Test2.<init>(Main.java:31)
    at Main.main(Main.java:12)
java.lang.NullPointerException
    at Main$Test3.<init>(Main.java:38)
    at Main.main(Main.java:15)

Solution 2

<init>

is called

Instance Initialization method

which is created by your java compiler from the constructor you have defined. Though it is not valid method definition, your JVM expects this and anything that you put in the constructor will be executed in method. So when you an exception with from , you can be sure that it is from the constructor of the executed java class. Read more about this on Bill venner's design technique articles on Object Initialization.

Share:
10,715

Related videos on Youtube

Pops
Author by

Pops

Former Community Manager at Stack Exchange (August 2013-November 2017). My posts from before or after that time period (and, like, a bunch of the ones from during it, too) should not be considered "official" in any way. Joel: I have all these opinions ... and no outlet for them! Josh: Have you tried yelling them at the Internet? Joel: Almost exclusively! And yet problems still persist! -"The Grand Opining", HijiNKS ENSUE, by Joel Watson "On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." -Charles Babbage Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law. -Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

Updated on September 14, 2022

Comments

  • Pops
    Pops over 1 year

    What does <init> signify in a Java exception?

    For example:

    BlahBlahException...
    
    at java.io.FileInputStream.<init>(FileInputStream.java:20)
    
    • Jon Skeet
      Jon Skeet over 11 years
      Quick note - your tag of "compiler-errors" suggests you may not be drawing an appropriate distinction between compile-time errors and execution-time exceptions. This isn't a compiler-error, it's an exception. (I'll edit the tag now, but it's something to be aware of.)