Java/ Try & Catch

+2 Arnab Banerjee · November 6, 2014
What is meant by the terms 'try' and 'catch' in java? Explain in simple manner ??

Post a Reply

Replies

Oldest  Newest  Rating
+2 Will Moffat · November 10, 2014
Basically, if you have a bit of code that might cause an error, you would surround it with a try {} statement.

System.out.println(5/0);

This would throw an ArithmeticException, because dividing by zero doesn't work (for maths reasons). This would spam the error with a very big error. In order to manage the errors, we use try and catch statements.
try {
System.out.println(5/0);
} catch(ArithmeticException e) {
System.out.println("Tried to divide by zero, and that threw an error, so I'm now running the code inside the catch block.");
}

The bit inside the catch paranthasese is the Exception we are trying to catch, followed by an identifier.
+2 Bucky Roberts · November 6, 2014
Here is a video explaining it.

0 Arnab Banerjee · November 10, 2014
THANKS BUCKY & WILLIAM.
  • 1

Java / Android Development

107,260 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator