What is the use of "Exception e" and why is it need to be "Exception e"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the use of "Exception e" and why is it need to be "Exception e"?

I saw it is used to try and catch errors or exceptions, but could someone better explain it? Going over the lesson again made me more confused..

19th Jul 2016, 8:54 AM
Tristan
3 Answers
+ 2
Hello exception is the method that you make it to prevent an error in the code to make the program stops like if i make a calculator and i give the user to write a number (int) so in the code i make a varaibale to store the value to print it to him so if he put a char or string the program will stops because the varaibale int it was waiting the user to put int to stores it and the user put in the box another value so the program will stop because like you know if i make int x; it cannot stores char or char h; it cannot stores int so i make this code inside try and catch if the user put right value it will not make anything with catch block like it will be hidden but if there is anything and the user but wrong type in the box like write the number like two it will go and make the codes inside catch you can make inside the block print please enter only numbers like that (Exception e) this e like an object to use the things of exception like if i said in the catch block print e.message it will print the type of exception the e we can deal with theException orders and know it
19th Jul 2016, 9:08 AM
Mostafa Abobakr
Mostafa Abobakr - avatar
0
Thanks.
19th Jul 2016, 9:17 AM
Tristan
0
Just to add.... exception e is the base class for exceptions and is kind of a catch all exceptions class. it can be extended to create your own exception class or simply use it to catch any type of exception, this is not recommended, if you need to log your exceptions for debugging it helps using specific exception classes such as RuntimeException. By doing this, you can handle specific exceptions accordingly.
21st Jul 2016, 5:54 AM
Eric Gitangu
Eric Gitangu - avatar