throwsclause is used to declare an exception andthrowkeyword is used to throw an exception explicitly.- If we see syntax wise then
throwis followed by an instance variable andthrowsis followed by exception class names. - The keyword
throwis used inside method body to invoke an exception andthrowsclause is used in method declaration (signature).
For example
throw
throw new Exception("You have some exception")
throw new IOException("Connection failed!!")
throws
public int myMethod() throws IOException, ArithmeticException, NullPointerException {}
- You cannot declare multiple exceptions with
throw. You can declare multiple exception e.g. public void method()throws IOException,SQLException. - checked exceptions can not be propagated with
throwonly because it is explicitly used to throw an particular exception. checked exception can be propagated withthrows.
Comments
Post a Comment