Why is the "throwing an exception" necessary when calling a function?

class throwseg1 { void show() throws Exception { throw new Exception("my.own.Exception"); } void show2() throws Exception // Why throws is necessary here ? { show(); } void show3() throws Exception // Why throws is necessary here ? { show2(); } public static void main(String s[]) throws Exception // Why throws is necessary here ? { throwseg1 o1 = new throwseg1(); o1.show3(); } } 

Why show2() compiler report that the show2() , show3() and main() methods have

unregistered exception An exception that must be detected or declared thrown

when do I remove throws Exception from these methods?

+84
java exception-handling throws checked-exceptions unhandled-exception
Jul 21 2018-12-12T00:
source share
8 answers

In Java, as you probably know, exceptions can be divided into two parts: one that needs a throws or needs to be handled if you don't specify one and the other doesn't. Now look at the following figure:

enter image description here

In Java, you can throw away anything that extends the Throwable class. However, you do not need to specify a throws for all classes. In particular, classes that are Error or RuntimeException or any of the subclasses of the two. In your case, Exception not a subclass of Error or RuntimeException . So this is a checked exception, which should be indicated in the throws if you have not handled this particular exception. That's why you needed a throws .




From the Java Tutorial :

An exception is an event that occurs during program execution and violates the normal flow of program instructions.

Now, as you know, exceptions are divided into two parts: verified and unverified. Why are these classifications?

Checked exception: They are used to represent problems that can be resolved during program execution. They are usually not a programmer error. For example, a user-specified file is not readable, or there is no network connection available, etc. In all these cases, our program does not need to exit; instead, it can perform actions, such as notifying the user, or go to the rollback mechanism. (for example, working offline when the network is unavailable), etc.

Unchecked exceptions: They can again be divided into two parts: errors and RuntimeException exceptions. One of the reasons that they do not need to be checked is that there are many of them, and their need to process all of them will clutter up our program and reduce its clarity. Another reason:

  • Runtime Exceptions: These usually occur due to a programmer error. For example, if an ArithmeticException division by zero or an ArrayIndexOutOfBoundsException occurs, then this is because we are not careful enough in our coding. Usually they occur due to some errors in the logic of our program. Therefore, they must be cleaned before our program enters operational mode. They are not tested in the sense that our program should fail when it happens, so that we programmers can resolve it during development and testing itself.

  • Errors: Errors are situations from which a program usually cannot recover. For example, if a QaruError occurs, our program cannot do much, for example, to increase the size of the stack of calls to program functions. Or, if an OutOfMemoryError occurs, we cannot do much to increase the amount of RAM available for our program. In such cases, it is better to exit the program. That is why they are made untested.

For more information, see:

+131
Jul 21 '12 at 4:11
source share

Java requires that you handle or declare all exceptions. If you do not handle the exception using the try / catch block, it must be declared in the method signature.

For example:

 class throwseg1 { void show() throws Exception { throw new Exception(); } } 

Must be written as:

 class throwseg1 { void show() { try { throw new Exception(); } catch(Exception e) { // code to handle the exception } } } 

This way you can get rid of the throws Exception declaration in the method declaration.

+23
Jul 21 '12 at 4:13
source share

Exception is a proven exception class. Therefore, any code that invokes a method that declares that it throws Exception should handle or declare it.

+3
Jul 21 2018-12-12T00:
source share

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.

+3
Nov 15 '13 at 18:18
source share
 package javaexception; public class JavaException { void show() throws Exception { throw new Exception("my.own.Exception"); } void show2() throws Exception // Why throws is necessary here ? { show(); } void show3() throws Exception // Why throws is necessary here ? { show2(); } public static void main(String[] args) { JavaException a = new JavaException(); try{ a.show3(); }catch(Exception e){ System.out.println(e.getMessage()); } } 

Only small changes to your program. It seems that many people misunderstand the main problem when you make an exception that you need to handle, not necessarily in one place (for example, the show1,2,3 method in your program), but you must first use the call method inside the "main" one. in a word, there is a "throw", there must be a "catch / try", even if not the same method where the exception occurs.

0
Mar 15 '18 at 8:11
source share
 void show() throws Exception { throw new Exception("my.own.Exception"); } 

Since the show () method checks for an exception that is not handled in this method, we therefore use the throw keyword to propagate the Exception.

 void show2() throws Exception //Why throws is necessary here ? { show(); } 

Since you are using the show () method in the show2 () method and you have thrown an exception, at least you should work here. If you do not handle the Exception here, you use the throw keyword. Therefore, this is the reason for using the throw keyword in the method signature.

0
Apr 08 '18 at 17:43
source share

If you throw an exception by declaring the throws directive in the signature of the current method, then somewhere up the line or call stack the try / catch construct must be used to handle the exception.

0
Jul 26 '19 at 12:07 on
source share

Essentially, if you do not handle the exception in the same place where you throw it, then you can use the โ€œexception throwโ€ in the function definition.

-one
Jun 18 '19 at 12:53 on
source share



All Articles