-2

can we create custom exception extending class other than Exception and RuntimeException?

milez
  • 2,201
  • 12
  • 31
manish
  • 1
  • 2
  • You can try it for yourself and find out. For the record, exceptions are also classes and they can be extended if not final. – Codebender Aug 10 '15 at 12:56
  • Thanks but can any subclass of Exception or Runtimeexception is suitable for writing Custom Exception? or it might face problems in catching exceptions in later scenarios? – manish Aug 10 '15 at 13:07
  • Both can be used to create custom Exceptions. The decision depends on whether you want your exception to be checked (Exception) or unchecked (RuntimeException). Find some resources here: https://docs.oracle.com/javase/tutorial/essential/exceptions/ – Fildor Aug 10 '15 at 13:49

2 Answers2

0

You can extend Error or directly Throwable

cahen
  • 15,807
  • 13
  • 47
  • 78
  • Thanks but can any subclass of Exception or Runtimeexception is suitable for writing Custom Exception? – manish Aug 10 '15 at 13:05
  • @shekharsuman could you elaborate that? It seems like an answer to me, it says he can do what he wants with 2 examples – cahen Aug 10 '15 at 18:16
  • @CarlosRodriguez - Sorry, I incorrectly flagged it as "not being an answer". I apologise for the same. – Am_I_Helpful Aug 10 '15 at 18:26
0

You can extend any class that is not final. Exceptions are no exceptions.

Dimitar
  • 134
  • 1
  • 6
  • can any subclass of Exception or Runtimeexception is suitable for writing Custom Exception? or it might face problems in catching exceptions in later scenarios? – manish Aug 10 '15 at 13:08
  • I suggest you brush up on what the difference between checked and unchecked exception is (http://stackoverflow.com/questions/14011749/checked-vs-unchecked-exceptions-in-java). Because yes, sub-classing Exception and RuntimeException will have an impact on how you write your code (more specifically, whether you need to catch your exception, declare it or do nothing in particular) – Dimitar Aug 10 '15 at 13:12