+ 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.
9 Respostas
+ 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.
+ 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
+ 2
this is for unexpected errors or missing somthing in your code
+ 2
just the name of refrence.. because the object is class is exception so object name is start as e...
+ 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
+ 2
'e' is just a parameter its means catch block can recieve an argument and the Data type of argument is exception datatype
+ 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.
0
e is a object of exception class
0
If we don't catch exception
Exception catching is a good practice