Reputation:
I am a newbie in programming. I was learning java and I found the try/catch. I understood the and the concept very well but I have a question with the catch sentence: What does the identifier do in the catch sentence?, How does it work? How do I use it?
If you do not understand:
public class Example {
public static void main (String[] args) {
try {
Integer.parseInt("m");
}catch (Exception e) {System.out.println("ERROR");} //This identifier (e)
}
}
Probably the answer is very obvious but i want be sure.
Upvotes: 0
Views: 194
Reputation: 27
The catch catches Exception e. This means that the exception returns an Exception object with the identifier e. This is assigned by the user and you could do catch (Exception caughtException). Exceptions have methods and can be used in the catch block. For example System.out.println(e) will print the details of the Exception method.
Upvotes: -1
Reputation: 533880
The e
is the exception you should be examining or at least printing so that you know what error occurred, where and possibly why. add
e.printStackTrace();
Upvotes: 2