java - Program throws Error
the following program compiles correctly. causing stack overflow error? how exception pushed on stack?
public class reluctant { private reluctant internalinstance = new reluctant(); public reluctant() throws exception { throw new exception("i’m not coming out"); } public static void main(string[] args) { try { reluctant b = new reluctant(); system.out.println("surprise!"); } catch (exception ex) { system.out.println("i told so"); } } }
you have field initialization code automatically added constructor body javac compiler. constructor looks this:
private reluctant internalinstance; public reluctant() throws exception { internalinstance = new reluctant(); throw new exception("i’m not coming out"); }
so calls recursively.
Comments
Post a Comment