0

I am curious as to how certain exceptions fall into checked exception category whereas others fall on the other ? I know the basic difference between compile time error (syntactical error etc - this is intuitive and understandable.) and runtime error(null pointer exception etc.). But I will prefer a more detailed analysis , as to how compile time error is detected and why does IO exception falls in compile time , as Input and output is something that is expected only during runtime ? Also some discussion on Illegal Argument exception is detected?

Echo
  • 570
  • 1
  • 7
  • 21
  • 1
    _All_ exceptions happen at runtime. The compiler only checks statically that you have code that _handles_ checked exceptions. – Marko Topolnik Dec 03 '16 at 09:43
  • @Joe C , I have updated the question statement . Hope now it serves its purpose. – Echo Dec 03 '16 at 15:41

4 Answers4

2

I don't think there is a rule. In old times, checked exceptions was way to go, but today people tend to like unchecked exceptions better. You will probably find a lot of discussions around this topic on the internet. You can think of checked exceptions as: "You really need to handle this here or else things will go really bad for you". Personally I usually convert checked exceptions to unchecked exceptions if I need to handle them further up the application stack.

Einar
  • 181
  • 1
  • 7
  • 1
    It's somewhat arbitrary, so expecting to find some definite rule is a waste of time. Some people think that checked exceptions were a bad idea to begin with, etc. – Kayaman Dec 03 '16 at 09:42
1

At the VERY core, a "checked" exception is usually caused by external sources e.g. IOException (so at compile time) whereas a "runtime" exception can happen, well, anywhere and is usually dependent on variables (which can change at runtime). It is the programmers duty to prevent all runtime exceptions (checking for null, checking indexes etc) whereas a checked exception can be caused by e.g. a file being renamed/removed. These two have a lot of overlap and hence a specific 'border' is impossible to define.

Some exceptions have been decided to be labeled as runtime because forcing the programmer to catch and handle them each time would obfuscate the code way too much. Checked exceptions usually don't occur too often but when they do, need to handled accordingly (closing files in a finally block etc).

I don't believe there's a definite rule which separates the two, but there has definitely been quite some though process behind it.

Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29
0

How is IllegalArgumentException a runtime exception where IO exception is a checked exception ?

This is because IllegalArgumentException extends RuntimeException (called unchecked exceptions or runtime exceptions) whereas IOException extends Exception (called as checked exceptions).

In other words, if any exception object (is of type java.lang.Exception) is thrown from a method or code block then it will be verified at compile time whereas if an exception object is of type RuntimeException, then it will NOT be validated at compile time i.e., it will be known only during the actual execution (runtime) of the program.

Also, note that, checked exceptions are meant for compile time validations (i.e., we can provide a recovery mechanism by catching the exception) and the compiler ensures that all the checked exceptions are either thrown using throws keyword or caught using catch which is known as Catch or Specify, you can look here.

Vasu
  • 21,832
  • 11
  • 51
  • 67
0

Specifically it makes sense for IllegalArgumentException to be unchecked because it's thrown in a large number of core Java APIs. If an exception is thrown practically everywhere making it checked does no good.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276