0

I have below lines of code to catch the exceptions.

catch (Throwable ex) {
    //print stack trace         
}

and

catch (Exception ex) {
        //print stack trace         
    }

between the above two catch blocks, which one is recommended?

Thanks!

user755806
  • 6,565
  • 27
  • 106
  • 153

2 Answers2

3

Catching Exception is always recommended as Throwable will also catch the Errors. Generally Errors are something Fatal and you should not continue.

Rahul
  • 44,383
  • 11
  • 84
  • 103
G.S
  • 10,413
  • 7
  • 36
  • 52
  • 1
    For web service response can i use Throwable, because i will send customized response to clients. Thanks! – user755806 Nov 18 '13 at 11:01
  • Use your own exceptions in this case. Your customized exception shall be subclass of Exception. Refer http://stackoverflow.com/questions/1754315/how-to-create-our-own-exceptions-in-java – G.S Nov 18 '13 at 12:21
0

Exception is Sub class of Throwable.

Error and Exception are 2 subclasses of Throwable. As suggested Errors results in something fatal(and hence should not be handeled). And Exception is programmatic issues and should be handled by developer. And Hence catch(Exception e) is correct. Hope i made my point clear.

Dark Knight
  • 8,218
  • 4
  • 39
  • 58