Red Security
What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling? - Printable Version

+- Red Security (https://redsecurity.info/cc)
+-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5)
+--- Forum: Java (https://redsecurity.info/cc/forumdisplay.php?fid=7)
+--- Thread: What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling? (/showthread.php?tid=1437)



What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling? - Mr.Kurd - 03-20-2020

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:
  • 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
E.g.

throw

throw new Exception(“You have some exception”)

throw new IOException(“Connection failed!!”)

throws
throws IOException, NullPointerException, ArithmeticException


Wa Salam Alekum