Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling?

0 Replies, 5214 Views

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
Rs
* Thankful to Allah *
Kurdy

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Why Java Strings are immutable in nature? Mr.Kurd 0 2,649 03-20-2020, 08:47 PM
Last Post: Mr.Kurd
Question What is constructor chaining in Java? Mr.Kurd 0 2,603 03-20-2020, 08:43 PM
Last Post: Mr.Kurd
Question What is the difference between equals() and == in Java? Mr.Kurd 0 2,639 03-20-2020, 08:40 PM
Last Post: Mr.Kurd
  What public static void main(String args[]) stand for in Java? Mr.Kurd 0 2,678 03-20-2020, 08:38 PM
Last Post: Mr.Kurd
Question Why multiple inheritance is not supported in java? Mr.Kurd 0 2,617 03-20-2020, 08:34 PM
Last Post: Mr.Kurd
  Tutorial Taking Screenshot in Java Mr.Kurd 0 2,708 03-09-2020, 05:38 PM
Last Post: Mr.Kurd
  10 java skills HeavensReject 1 3,416 01-12-2020, 05:31 AM
Last Post: Mr.Kurd
Wink Tutorial Downloading video from Youtube using JAVA Mr.Kurd 0 5,412 02-06-2019, 06:33 PM
Last Post: Mr.Kurd
  Tutorial Java and SSH Mr.Kurd 0 3,428 12-16-2018, 08:05 PM
Last Post: Mr.Kurd
  Tutorial Writing to a word file in Java Mr.Kurd 0 3,111 11-30-2018, 08:43 AM
Last Post: Mr.Kurd



Users browsing this thread: 1 Guest(s)