Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hows the Exceptional object is converted in to one of Java checked Exception?

Tags:

java

exception

When we have a custom Exception, say SkillRequiredException, we check for some condition like checking the skill of the employee and if the condition fails we will throw SkillRequiredException. Till this I am fine and clear.

But lets take FileInputStream class. It throws FileNotFound checked exception. When I see FileInputStream source code, I could not see anywhere - checking for some condition (and) throw FileNotFoundException.

My question is, how JVM knows that file is not present and how the Exceptional Object created by JVM is identified as FileNotFoundException with out using throw FileNotFoundException in FileInputStream class?

like image 285
Sanjeevan Avatar asked Mar 18 '23 10:03

Sanjeevan


1 Answers

Well, if you check which methods are called by the constructor of FileInputStream, you'll see that it eventually calls :

private native void open(String name) throws FileNotFoundException;

This is a native method, which means it's not written in Java and you can't see its code, but it can still throw a FileNotFoundException exception.

like image 53
Eran Avatar answered Apr 06 '23 03:04

Eran