1

What is the difference between throws Exception and throws IOException?

Either of them works. So, what's the major difference in between them? What if one of them had not existed?

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 1
    Does this answer your question? [Why use IOexception instead of Exception when catching?](https://stackoverflow.com/questions/20644559/why-use-ioexception-instead-of-exception-when-catching) – kasptom Oct 25 '20 at 18:32
  • because handling the concrete problem is much better then handling all the problems at once. Exception is parent of all Exceptions, including RunTime onces :) – George Weekson Oct 25 '20 at 18:40

1 Answers1

1

This question is really about the basics of Java exception mechanism, but, strangely, I couldn't find an exact duplicate on StackOverflow...


These declarations tell the compiler (and the programmers) which type(s) of exceptions may be thrown by a method.

throws Exception

means that a method may throw any Exception (either an Exception instance directly, or any subtype of Exception, including IOException).

throws IOException 

tells that a method may throw an IOException, but not, for example, SQLException.

It is usually a good practice to declare specific exceptions, e.g. throws IOException, ParseException, instead of just writing throws Exception.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103