tags:

views:

138

answers:

2

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");
      }
      catch{
       System.out.println("C");
       throw new Exception("2");
      }
      finally{
       System.out.println("D");
       throw new Exception("3");
      }

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

    }
}
+6  A: 

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.

Daniel Pryden
I misread your answer and assumed you were talking about the lack of a **catch** clause. Sorry for the downvote; can't take it back now unless you edit.
Michael Petrotta
there you go, Michael
akf
Thanks, akf. I should have realized I could have edited it myself. My brain is a little slow right now; time for lunch, I guess.
Michael Petrotta
+11  A: 

catch must declare what exception it catches:

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