+ 1

how to use try and catch exceptions

13th Oct 2016, 5:20 PM
RHYAN DAVE ABORDO
RHYAN DAVE ABORDO - avatar
3 Answers
+ 9
try { //Insert the code here that might or cause an error. catch (Exception e) { //Manage how to handle it for example: System.out.println("An error occured"); } //Now whenever your code causes an error it outputs "An error occured".
16th Apr 2017, 4:51 AM
MrCoder
MrCoder - avatar
+ 2
It depends of you wanna do, for example try it's to try something that is not sure to be run as 100 percent of chance so if there was an error by trying to run the tried code the error will be caught by catch to a exception, in short it's to try something, else if the tried code hasn't been run caused by an error, the error is catch
13th Oct 2016, 8:26 PM
ZentsuGo
ZentsuGo - avatar
+ 1
You know that a part of your code might fail to excute. For example , you have FILE OPERATION. You know that the file you are looking for might not exist. And if it dont exist , your app will crash. So to prevent your app to crash you surround the lines of code which do the file operation in a TRY block and after TRY block you open a CATCH block. So if any line of code in TRY block fails to excute , it will stop excuting and start excuting the codes in CATCH block. So your app DOESN'T CRASH.
16th Oct 2016, 5:55 PM
AmirHosein Mansoori
AmirHosein Mansoori - avatar