-1

The question is about the result of the below code. The answer is compilation error. However I really do not understand why we can't have constructor in try/catch block. I will put the the code below:

public class Test {
    try {
        public Test() {
            System.out.println("GeeksforGeeks");
            throw new Exception();
        }
    }
    catch(Exception e) {
        System.out.println("GFG");
    }
    
    public static void main(String [] args) {
        Test test= new Test();
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
subhanceo
  • 74
  • 8
  • 2
    Because you can have a wallet inside your pocket, not a pocket inside a wallet. – Pavel Smirnov Sep 24 '20 at 23:22
  • Does that actually come from GeeksForGeeks? – Spectric Sep 24 '20 at 23:22
  • 1
    The classes body is not runnable (except static blocks) so it doesn't make sense to allow code that will run to be put there. The try/catch needs to go where the constructor is called i.e. the main function. – Paul Rooney Sep 24 '20 at 23:24
  • I came across on facebook but I do not know exact source @Aniox – subhanceo Sep 24 '20 at 23:31
  • @Paul Rooney how can I make it static blocks? – subhanceo Sep 24 '20 at 23:32
  • You can see [here](https://stackoverflow.com/questions/2943556/static-block-in-java) but why do you want to? You still wont be able to define the constructor in a static block. – Paul Rooney Sep 24 '20 at 23:34
  • so I know I can write try/catch in main. I am just curious about the different way. For instance, can I put try/catch inside the static block? What is the downside of it? – subhanceo Sep 24 '20 at 23:35
  • You're trying to put java code directly inside a class definition without putting it inside a method. – NomadMaker Sep 24 '20 at 23:57

2 Answers2

2

Because the assignments are statements and statements are allowed only inside blocks of code(methods, constructors, static initializers, etc.) here's the clean code

public class Test {

    public Test()throws Exception {
        System.out.println("GeeksforGeeks");
        throw new Exception();
    }



public static void main(String [] args) {
    try {
        Test test= new Test();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

ElMobark
  • 492
  • 4
  • 10
1

Because a constructor is a declaration, not a statement.

Your constructor can be called by other code, but merely declaring it does not execute it; that’s what new Test() does. Nothing is executed merely by declaring the constructor, so there is nothing that can throw an exception. Thus, there is nothing to catch.

In more general syntax terms, statements which don’t evaluate to a value can only exist in constructors, methods, and initialization blocks.

You can, however, do this:

public static void main(String[] args) {
    try {
        Test test = new Test();
    } catch (Exception e) {
        System.out.println(e);
    }
}

new Test() actually executes the constructor, which is why it may throw an exception and thus you can legally attempt to catch any exception it may throw. Syntactically, all of the above code is inside a method (the main method), which is allowed.

VGR
  • 40,506
  • 4
  • 48
  • 63