+ 1
how to use try and catch exceptions
3 Réponses
+ 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".
+ 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
+ 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.