We define user defined exceptions by extending the Exception class. Why not by extending RunTimeException class. I tried it and it doesn't give any exceptions. Then why we don't do it.
Printable View
We define user defined exceptions by extending the Exception class. Why not by extending RunTimeException class. I tried it and it doesn't give any exceptions. Then why we don't do it.
But you can do it if it makes sense.
Have a read of The Catch or Specify Requirement in Oracle's Tutorial. It describes checked exceptions, errors and runtime exceptions. They are different sorts of thing and you are free to extend whichever makes sense for the particular exceptional situation you wish to signal. (or use an instance of one of the already existing classes)