+ 2

Java RuntimeException detection

How come java RuntimeException are not being detected at compile time?

27th Apr 2020, 7:02 AM
ifl
ifl - avatar
4 ответов
+ 2
In java, exceptions are two types. 1)checked exception. 2) unchecked exception. Checked are checked or detected at compile time. Unchecked exception are not checked at compile time and it's just ignored by compiler. Because they arise at run time.. You cannot say that it is an error at compile time.. For example : opening a file will be checked at compile time for the file existence and possible manipulations.. If you give nonexistent file then it raise FileNotFoundException.. And if you specify a class name in an object creation which is not exist, it raises classNotfoundException, because at compile time it initialize objects.. But in unchecked exceptions are not detected at compile time. For example, You have expression a/b and you taking values at runtime so when only if you give 0 for b, then only it has to raise error otherwise not so it is raise ArithmeticException at runtime when you give b value 0. Similarly arrayIndexoutofbounds exception, compiler can't detect you are access beyond limit or not.
27th Apr 2020, 7:58 AM
Jayakrishna 🇮🇳
+ 1
Many programmers feel that you should not handle these exceptions in your programs because they represent the type of errors from which programs cannot reasonably be expected to recover while the program is running. When an unchecked exception is thrown, it is usually caused by a misuse of code - passing a null or otherwise incorrect argument.
27th Apr 2020, 7:45 AM
🔹Marko B.🔹
🔹Marko B.🔹 - avatar