Exception Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Exception Java

How do you choose between try/catch or Throws ?

7th Aug 2019, 2:24 PM
Supergirl
22 Answers
+ 3
ok many thanks 😊
8th Aug 2019, 3:39 PM
Supergirl
+ 2
thanks for your answer 😊 so if I well understand you choose try/catch when you are sure exception will occur
8th Aug 2019, 7:19 AM
Supergirl
+ 2
Hi
13th Aug 2019, 5:19 AM
Kirubha Karan
Kirubha Karan - avatar
+ 2
Hlo
14th Aug 2019, 3:50 AM
Kirubha Karan
Kirubha Karan - avatar
+ 1
I choose try/catch when I am not shure what exactly will happen. For example everything outside my program like user input, load/save files, connection to database or a server.... You can always read the docomentation (java doc or whatever libary u r using) to check what exceptions can occure.
8th Aug 2019, 3:04 PM
Michael F
Michael F - avatar
+ 1
Please come in chat
13th Aug 2019, 8:20 AM
Kirubha Karan
Kirubha Karan - avatar
+ 1
hi
13th Aug 2019, 8:32 AM
Supergirl
+ 1
Update app. In notifications there will be mesaages. In that mesaage me
13th Aug 2019, 8:44 AM
Kirubha Karan
Kirubha Karan - avatar
+ 1
many thanks 😊
19th Aug 2019, 3:56 PM
Supergirl
+ 1
pourquoi se venger ??? sur qui?
22nd Aug 2019, 2:18 AM
praveen kasojjala
praveen kasojjala - avatar
+ 1
it was for Java chalenges. I've lost against somebody yesterday
22nd Aug 2019, 8:47 AM
Boubacar Toure
Boubacar Toure - avatar
+ 1
Thanks Very clear explanations !!!
13th Dec 2019, 2:51 PM
Supergirl
+ 1
You use try when something may go wrong later, for example, using a audio file from external source, like this : https://code.sololearn.com/Wl23AIEG6SuZ/?ref=app Because the website may break down, or they remove the mp3 files. We use try-catch when loading the audio, and don't forget to use try-catch when trying the audio too (line 114 and 123). throw is similar to assertion, for example, a program only for 18 years old or above, and the user entered that his age is 16, now you can use throw to raise an error to terminate the program and give the user a warning.
7th Apr 2020, 1:42 PM
Gordon
Gordon - avatar
+ 1
Many thanks for explanations 😊
7th Apr 2020, 2:35 PM
Supergirl
0
Use try/catch to handle existing exceptions. And use throws to generate a new exception.
7th Aug 2019, 4:26 PM
Michael F
Michael F - avatar
0
Nice!
11th Aug 2019, 3:09 PM
Juan Debenedetti
Juan Debenedetti - avatar
0
Try catch is used when you want to catch and handle the exception. Throws is used to propagate the exception to the caller of the method then the caller will handle that exception..
19th Aug 2019, 11:41 AM
praveen kasojjala
praveen kasojjala - avatar
0
Je prendrais ma revenge
21st Aug 2019, 7:16 PM
Boubacar Toure
Boubacar Toure - avatar
0
Supergirl no problem ;) keep up learning 💪
7th Apr 2020, 2:42 PM
Gordon
Gordon - avatar
0
Meko batao yaar ek ans
3rd Jul 2020, 3:48 PM
Anikesh vinod khadse