+ 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
13 Respuestas
+ 1
Exemple of custom exception :
https://code.sololearn.com/cfPBTnUK1tfS/?ref=app
+ 3
Roland if we call constructor of Exception class does it invokes getMessage() function??
+ 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.
+ 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..
+ 2
Roland Jayakrishna🇮🇳 Thanks a lot for your help and support now I get this.
+ 1
Don't understand how the method divide can throw exception until user gives 0 to its 2nd parameter
+ 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.
+ 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.
+ 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.
+ 1
Jayakrishna🇮🇳 Yeah, I mean that when we call constructor of Exception class the message inside the super does goes to getMessage() function.
+ 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"
+ 1
Jayakrishna🇮🇳 Can you please explain the difference between toString() method and getMessage() method
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..