java - Why i can`t throw Exception(checked) in a method that is invoked in a try catch statement? -


 public class simple {     public static void main(string[] args) {         try {             system.out.print("hello ");             throwit();         } catch (exception re) {             system.out.print("caught ");         }     }      public static void throwit(){  // line number 11         throw new exception();  // line number 12     }    } 

why give me compile error in line number 12. if use throws exception line number 11 work fine. if throw subclass of exception(in line number 12) work properly... why so?...

i want know happen in side(how compiler shows error this)?

you have method there throwing checked exception, method signature doesn't specify able that. checked exceptions have declared in method signature, , explicitly handled try/catch blocks or rethrowing; that's definition of checked exception is. :)

this line:

public static void throwit() 

should be

public static void throwit() throws exception 

Comments