After studying more and more about throws
statement in Exception Handling
I am getting confused.I found-
If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception.
class Boxing1{
public static void main(String args[]) throws IOException{
new B().meth2();
System.out.println("33333333");
}
}
class A{
void meth1() throws IOException{
throw new IOException();
//throw new NullPointerException();
//System.out.println("111111111111");
}
}
class B{
void meth2() throws IOException{
new A().meth1();
System.out.println("2222222");
}
}
Instead of using throws there is still an exception- My console is showing following error-
Exception in thread "main" java.io.IOException
at A.meth1(Boxing1.java:17)
at B.meth2(Boxing1.java:24)
at Boxing1.main(Boxing1.java:10)
Untill I am not putting calling of meth1 in try-catch block there is a exception in spite of using throws. What is the role of throws here?
try{new A().meth1();}catch(Exception e){System.out.println(e);}
I needed your confirmation on it.I am confused.My one line Query is-
Is there any other role of throws
instead of propagating a CheckedException
?