Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why FileNotFoundException is CheckedException?

I know FileNotFound is Checked Exception but though it is, only during the Run time this exception will occur.It is more like Arithmetic Exception(Unchecked).

Whether it is checked or unchecked the exception will happen only during runtime.

My Question is why we call the FileNotFound/IO/DB related stuffs as Checked Exception?

Please share me your valuable thoughts :)

like image 469
Lathy Avatar asked Mar 06 '15 09:03

Lathy


People also ask

Why is FileNotFoundException a checked exception if it is encountered during run time?

Checked or unchecked means whether it is forced to handle at compile time or it will only be identified when it is encountered at runtime. If an exception is checked means compiler has way to identify whether the exception can occur or not.

What type of exception is FileNotFoundException?

FileNotFoundException is another exception class available in the java.io package. The exception occurs when we try to access that file which is not available in the system. It is a checked exception because it occurs at run time, not compile-time, and it is thrown by one of the following constructors: RandomAccessFile.

Which of the following is are checked exceptions FileNotFoundException?

Answer: Explanation: In Java, the FileNotFoundException is a checked exception. When we try to read a file from the filesystem, Java compels us to deal with an error circumstance in which the file may not exist.

Why are exceptions checked?

In broad terms, a checked exception (also called a logical exception) in Java is something that has gone wrong in your code and is potentially recoverable. For example, if there's a client error when calling another API, we could retry from that exception and see if the API is back up and running the second time.


Video Answer


2 Answers

Exceptions always encountered at runtime only, Difference is made when a exception is handled.

Checked or unchecked means whether it is forced to handle at compile time or it will only be identified when it is encountered at runtime.

If an exception is checked means compiler has way to identify whether the exception can occur or not. and whenever you compile it, you will be forced to handle a checked exception and by doing this chances of runtime exceptions will be reduced.

During file handling, compiler doesn't check whether file is present or not, it just check whether you have handled fileNotFoundException or not, because once you are dealing with a file chances of encountering this exception is very high and you should handle it in your code. for arithmetic Exception there is not way to find it during compile time. and thus it is left unchecked.

like image 117
Kavan Avatar answered Oct 04 '22 08:10

Kavan


NullPointerException or ArithmeticException usually shouldn't happen in a finished, correct program. You can handle those just with checking before with if to see if you divide by 0 or an object is null and then you are sure this Exception will not be thrown. Every time handling those Exceptions can make the code less readable.

Now you could argue that you could do the same for FileNotFoundException by just checking if the file exists before doing anything. But many constructors or methods that expect a File also support String from which the file is created then. I guess it is a question of where you draw the line, if you always only have the File method and never also support String then I would have added it to the unchecked ones too I think.

In other words: if a FileNotFoundException is thrown then it can be the desired behaviour and drive the flow of your program, but NullPoinerException really shouldn't be used for that.

like image 32
maraca Avatar answered Oct 04 '22 09:10

maraca