Exceptionand IOExceptionboth are compile-time-dependent exceptions.
Exception
IOException
But we cannot use IOExceptionin the catch block. But we can use Exceptionin the catch block, what is the reason for this.
import java.io.*; class Demo{ public static void main(String args[]){ try{ }catch(IOException e){ // Does not compile } try{ }catch(Exception e){ // Compile } } }
You cannot catch a checked exception that is never thrown into a try block other than Exception(or Throwable). This behavior is defined by JLS, section 11.2.3 :
Throwable
, catch E1, , try, catch, , E1, E1 .
, , IOException ... , Exception, , IOException, RuntimeException .. catching, RuntimeException , , . IOException - , , , , , , .
See, the main reason is that you can catch any RuntimeException, it doesn't matter if it is selected from the try block or not, and RuntimeException is a class that extends the Exception class. So, as a parent class, catch "Exception" is always allowed. In your case, an exception thrown by an IOException is only allowed if you try to block the probability of it being thrown.
Source: https://habr.com/ru/post/1606460/More articles:Как сохранить целочисленные типы данных при экспорте в JSON? - jsonHow can I do one way in Linux? - linuxRecyclerView.Adapter onCreateViewHolder and methods onBindViewHolder do not get - androidDefine a list of packages other than the OS installed on a RedHat Linux computer - linuxOne-way file - linuxInno Service Request Windows Service Status - inno-setupInheritance from an obsolete class - c ++Inno Setup Detects Windows Firewall Status - securityGet an error while trying to use the `\ 1` that come from regex` (\ d)? `In Python - pythonFor line in: do not return all lines - pythonAll Articles