What's the main difference between throws and throw keyword in java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the main difference between throws and throw keyword in java ?

6th Dec 2017, 12:01 AM
Yogesh Keshari
Yogesh Keshari - avatar
3 Answers
+ 4
The throws keyword is used to mark a method which will throw a exception. By doing this, the user must handle that exception. This lets them handle the exception in their own way. The throw keyword raises that exception, i.e. causes the exception to happen. Example: public void myMethod() throws IOException { try { do some Input/output which may cause a IO exception. } catch (IOException e) { throw e; } } public void myMethod2() { try { myMethod(); } catch (IOException e) { Handle the error here. } }
6th Dec 2017, 12:11 AM
Hassie
Hassie - avatar
+ 1
Kindly vote my question..
6th Dec 2017, 2:38 AM
Yogesh Keshari
Yogesh Keshari - avatar
+ 1
exception that is thrown out of method is specified by throws. to create exception intentionally inside member fun we use throw.
6th Dec 2017, 6:56 AM
Thriveni
Thriveni - avatar