5

What does the e mean in the following code?

try { // Do something } catch (Exception e) { // Do something } 

I've been researching and have gotten nothing.

System.out.println("Thanks!");

5
  • 2
    e is a reference to the instance of the Exception, like s would be a reference to an instance of String when you declare like String s = "...";. Otherwise you won't be able to reference the exception and learn what's wrong with your code Commented Nov 12, 2018 at 22:55
  • 1
    @MadProgrammer so it doesn't have to be e? Could it be written as some other letter and still work? Commented Nov 12, 2018 at 22:56
  • 1
    The Exceptions lesson might provide you with better/more information Commented Nov 12, 2018 at 22:56
  • 2
    No, it doesn't have to be e. I often use exp, but it's kind of a personal thing. It's just the variable name Commented Nov 12, 2018 at 22:57
  • You can name your variables as you please Commented Nov 12, 2018 at 22:57

2 Answers 2

10

It's a variable name. Exception is the type. e is the name. You can use a different name. You might display a message to the user (or a stack trace).

try { // Do something } catch (Exception ohNo) { System.out.printf("Caught exception %s doing something.%n", ohNo.toString()); ohNo.printStackTrace(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

can you please check my question? i think you can help me on exception handling. thank you stackoverflow.com/questions/69747085/java-try-catch-exception
1

It is a object which contain info about an error happend. Inherit from throwable and give you a clear message of why your code went wrong

More info

enter link description here

enter link description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.