-3

What ExceptionType should be used in Java to catch any exception that occurs in try block. I just want to know that there is error in try block so that i can exit the program

try {

} catch (ExceptionType name) {

}
N Kumar
  • 1,302
  • 1
  • 18
  • 25
  • 1
    Please go through the basic – Rahman Oct 07 '15 at 08:33
  • can you elaborate what you actually asking? – SSH Oct 07 '15 at 08:33
  • Possible duplicate of [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – Kumar Saurabh Oct 07 '15 at 08:34
  • Use the exception type that's being thrown from the method call. for e.g. when doing IO operation, most of JDK api throws IOException so catch that exception. – SMA Oct 07 '15 at 08:34

2 Answers2

4

You can use java.lang.Exception to catch most exceptions. Whether you should or not is debatable: perhaps a function higher up the call stack can deal with the exception more appropriately.

The true catch-all is java.lang.Thowable. But take care when intercepting that as doing so can interfere with the workings of the JVM.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You can catch java.lang.Exception or java.lang.Throwable. Be aware, that it is anti-pattern to catch all Exceptions.

Natalia
  • 4,362
  • 24
  • 25