I have successfully implemented a custom exception using below code
CarNotFoundException.Java
public class CarNotFoundException extends Exception
{
private static final long serialVersionUID = 1L;
public CarNotFoundException(String msg) {
super(msg);
}
}
Car.Java
public static CarProvider getInstanceByProvider(String provider) throws CarNotFoundException {
if(!provider.equals(Constants.BMW || Constants.B||Constants.C{
throw new CarNotFoundException("Car Not Found");
}
return carProvider;
}
CarTest.java
try
{
carProvider = Car.getInstanceByProvider(provider);
} catch (CarNotFoundException e) {
e.printstacktrace();
}
What I want to do ?
Instead of e.printStackTrace(); when I calle.getMessage(),
I get nothing(blank).
How I can make custom e.getMessage() ?
Edit : I got my answer, I missed System.out.println()
Thanks for helping..!
CarNotFoundException, but you're catching aProviderNotFoundException. Thee.getMessage()will work just fine with the code inCarNotFoundException.e.getMessage()will get the message. Are you printing it?System.out.println(e.getMessage());