throws Exception is an automated way to track methods that may throw an exception for expected but inevitable reasons. Typically, a declaration refers to the type or types of exceptions that can be thrown, for example, throws IOException or throws IOException, MyException .
We all either end up writing code that stops unexpectedly and reports an exception due to what we did not expect before the program started, for example division by zero or an index outside. Since errors were not expected using the method, they could not be "caught" and processed by the try catch clause. Any unsuspecting users of this method are also unaware of this feature, and their programs will also stop.
When a programmer knows that certain types of errors may occur, but they would like to handle these exceptions outside the method, the method can "throw" one or more types of exceptions from the calling method instead of processing them. If the programmer did not declare that the method (may) throw an exception (or if Java did not have the opportunity to declare it), the compiler could not know, and this could be due to the future user of the method, catching and handling any exceptions that the method might throw. Because programs can have many levels of methods written by many different programs, it becomes difficult (impossible) to keep track of which methods can throw exceptions.
Despite the fact that Java has the ability to declare exceptions, you can still write a new method with unhandled and undeclared exceptions, and Java will compile it, and you can run it and hope for the best. What Java will not allow you to do is compile your new method if it uses a method declared as an exception (s) exceptions, if you do not handle the declared exception (s) in your method or declare your method as throwing the same exception (s), or if there are several exceptions, you can handle some and throw the rest.
When a programmer states that a method throws a specific type of exception, it is simply an automatic way to alert other programmers using the method so that an exception is possible. Then the programmer may decide to handle the exception or pass a warning by declaring the calling method, also throwing the same exception. Since the compiler was warned, an exception is possible in this new method, it can automatically check whether future callers of the new method handle the exception or declare it and ensure the execution of an event.
The best part about this solution is that when the compiler reports Error: Unhandled exception type java.io.IOException , it gives the file number and line of the method that was declared to throw the exception. Then you can simply simply pass the dollar and declare your method also throwing an IOException. This can be done up to the main method, as a result of which the program will stop and inform the user about it. However, it is better to catch the exception and deal with it nicely, for example, explain to the user what happened and how to fix it. When a method executes a catch and handles an exception, it no longer needs to declare an exception. Buck stops there to talk.