+ 1

Please help me to resolve this error.

I saw a tutorial on YouTube in he did the same thing made a custom Exception and throw it inside the method which is being called but when I try to do it gives me an error. And also for better clarification, he using IntelliJ IDEA. https://code.sololearn.com/c55BE0lpToKQ/?ref=app

18th Jul 2022, 2:10 PM
Amul Gaurav
Amul Gaurav - avatar
13 ответов
+ 1
19th Jul 2022, 5:22 AM
Roland
Roland - avatar
+ 3
Roland if we call constructor of Exception class does it invokes getMessage() function??
19th Jul 2022, 5:29 AM
Amul Gaurav
Amul Gaurav - avatar
+ 2
toString() is a global method for all objects which returns by default the hashcode of the object. It can be overridden to return a custom string of the object. getMessage() is one of the exception-specific methods. It allows to have the main message of the error, for a diffusion at the front for example. There are other methods like printStackTrace() which allows to have the whole stack of the error or getCause() which allows to locate which method triggered the error.
19th Jul 2022, 6:40 PM
Roland
Roland - avatar
+ 2
Yes. toString() is defined in Object class which is super class for any class in java *implicitly.. whenever you print an object, then toString() is automatically called like catch(Exception e) { System.out.println(e); here you are printing object but e.toString method is automatically called. Where getMessage() is defined in Exception class. You should call it manually as e.getMessage();.. Hope it helps..
19th Jul 2022, 7:28 PM
Jayakrishna 🇮🇳
+ 2
Roland Jayakrishna🇮🇳 Thanks a lot for your help and support now I get this.
20th Jul 2022, 2:16 AM
Amul Gaurav
Amul Gaurav - avatar
+ 1
Don't understand how the method divide can throw exception until user gives 0 to its 2nd parameter
18th Jul 2022, 3:47 PM
Amul Gaurav
Amul Gaurav - avatar
+ 1
Logic is throwing when 2nd parameter is 0; otherwise don't. you are throwing exception outside try{}, so your method must declare it as may throws exception.
18th Jul 2022, 4:13 PM
Jayakrishna 🇮🇳
+ 1
I am also thinking to wrap throw in try-catch Block but I saw a tutorial on YouTube who follows the same code as I do above and his progress program was running fine on IntelliJ IDEA that's why I am bit confused. Do you have some explanation why it is not running here on Sololearn or I am doing something wrong otherwise.
18th Jul 2022, 5:08 PM
Amul Gaurav
Amul Gaurav - avatar
+ 1
You may missing something there in code in video, I think. Call to constructor of Exception by super("msg") will just set the message of super class Exception. Does not call getMessage(); you need to call it explicitly when you need.
19th Jul 2022, 9:02 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Yeah, I mean that when we call constructor of Exception class the message inside the super does goes to getMessage() function.
19th Jul 2022, 9:05 AM
Amul Gaurav
Amul Gaurav - avatar
+ 1
constrctor be like Exception( String msg) { message = msg ; } And method be like : public String getMessage() { return message ; } e = new Exception("MyException") ; will call constructor e.getMessage() ; will return message "MyMessage"
19th Jul 2022, 9:33 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Can you please explain the difference between toString() method and getMessage() method
19th Jul 2022, 9:46 AM
Amul Gaurav
Amul Gaurav - avatar
0
public static int divide(int a, int b) throws MyException { if(b == 0) { throw new MyException(); } int c = a / b; return c; } //report method divide can throw exception before throwing exception..
18th Jul 2022, 2:38 PM
Jayakrishna 🇮🇳