+ 6

Why we write e in catch in java exception,what is the meaning of e?

catch(Exception e) { code of block } what is e here in catch brackets.

25th Aug 2018, 5:54 PM
Maninder $ingh
Maninder $ingh - avatar
9 Respuestas
+ 9
'e' is just a variable. ('e' stands for exception, but you can rename it anything you like, however, the data type has to remain 'Exception') The 'e' variable stores an exception-type object in this case. Your 'catch' block will literally 'catch'' an exception object that was 'thrown' at some point during a 'try' block and store it in the 'e' variable (a.k.a parameter) Inside your catch block, you can choose to throw 'e' again and some higher-level process can catch the exception object (like yours can/did). If there are no other catch blocks at a higher level when it is thrown, the exception will cause the program to halt.
25th Aug 2018, 11:59 PM
Justin Kindrix
Justin Kindrix - avatar
+ 4
You can think of it in the same way as a method parameter - you define a type followed by an identifier. e is just the name of the Exception in the catch block, you can give it whichever name you like
25th Aug 2018, 5:56 PM
Dan Walker
Dan Walker - avatar
+ 2
this is for unexpected errors or missing somthing in your code
25th Aug 2018, 10:02 PM
Alireza
Alireza - avatar
+ 2
just the name of refrence.. because the object is class is exception so object name is start as e...
26th Aug 2018, 10:12 AM
Aarav Raj
Aarav Raj - avatar
+ 2
catch(Exception e) { System.out.println(e); } in this "e" is reference variable. exception pass to the output. you can pass any reference value like c/e/n as your wish
26th Aug 2018, 2:44 PM
Venkatesh
+ 2
'e' is just a parameter its means catch block can recieve an argument and the Data type of argument is exception datatype
7th Sep 2018, 10:08 AM
Real Gutch
Real Gutch - avatar
+ 1
It's explained in the above comments already. I will just give an example how it can be used in a code. For example, consider we are catching an Arithmetic Exception while dividing a number by 0. catch(ArithmeticException e) { System.out.println(e.toString()); throw new ArithmeticException(); } This will print a string in Stack Trace, because of e.toString(). java.lang.ArithmeticException: / by zero So it's fair to say that e is a variable which store some information about the Arithmetic Exception. Hope it helps.
6th May 2020, 3:14 AM
Shubham Saurav
Shubham Saurav - avatar
0
e is a object of exception class
26th Aug 2018, 2:13 AM
Sukanya Maji
Sukanya Maji - avatar
0
If we don't catch exception Exception catching is a good practice
9th Feb 2019, 3:29 PM
Vivek Gajera
Vivek Gajera - avatar