开发者

"misplaced construct(s)" in Java code near a catch

开发者 https://www.devze.com 2022-12-10 06:13 出处:网络
I could not compile the following code in Java, error is: misplaced construct(s). What\'s wrong? public class ExceptionsTutorial {

I could not compile the following code in Java, error is: misplaced construct(s). What's wrong?

public class ExceptionsTutorial {

    public static void main(String[] argv) throws Exception{

        try{
            System.out.println("A");

            try{
                System.out.println("B");
                throw new Exception("1");
            }
      开发者_StackOverflow      catch{
                System.out.println("C");
                throw new Exception("2");
            }
            finally{
                System.out.println("D");
                throw new Exception("3");
            }

        }
        finally{
            System.out.println("F");
        }

    }
}


catch must declare what exception it catches:

catch (Exception E) {
 System.out.println("C");
 throw new Exception("2");
}


Read up on Java catch blocks. There is a required element that is missing in your code.

Note that Java's behavior is slightly different from that of C# or Python in this regard.

0

精彩评论

暂无评论...
验证码 换一张
取 消