03-20-2020, 08:49 PM
In The Name OF Allah
Al-Salam Alekum
What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling?
Quote:Following are the differences between two:
E.g.
- throw keyword is used to throw Exception from any method or static block whereas throws is used to indicate that which Exception can possibly be thrown by this method
- If any method throws checked Exception, then caller can either handle this exception(using try catch block )or can re throw it by declaring another ‘throws’ clause in method declaration.
- throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method
throw
throw new Exception(“You have some exception”)
throw new IOException(“Connection failed!!”)
throws
throws IOException, NullPointerException, ArithmeticException
Wa Salam Alekum