Is there a different role for throws instead of propagating a checked Exception?

After learning more and more about throws

in Exception Handling

, I am confused. I found -

If a method is capable of throwing an exception, the handle must specify this behavior so that callers of the method can protect themselves from this exception.

class Boxing1{  
public static void main(String args[]) throws IOException{
    new B().meth2();
    System.out.println("33333333");
}
}
class A{

    void meth1() throws IOException{
        throw new IOException();
        //throw new NullPointerException();
        //System.out.println("111111111111");
    }
}
class B{
    void meth2() throws IOException{
        new A().meth1();
        System.out.println("2222222");
    }
}

      

Instead of using throws, there is still an exception. My console is showing the following error:

Exception in thread "main" java.io.IOException
    at A.meth1(Boxing1.java:17)
    at B.meth2(Boxing1.java:24)
    at Boxing1.main(Boxing1.java:10)

      

Until I put the call to meth1 in the try-catch block, there is an exception despite the use of throws. What is the role of the throws here?

try{new A().meth1();}catch(Exception e){System.out.println(e);}

      

I need your confirmation. I'm confused. My one line Query is

Is there a role other throws

than distribution CheckedException

?

+3


source to share


3 answers


As you said, there are 2 kinds of exceptions.

  • "Checked exceptions" require developer attention. ... When you use a method that throws a checked exception (i.e. IOException

    ), you need to handle it (i.e. catch it) or propagate to the caller (i.e. throw), assuming the caller will catch it.

  • "Unchecked exceptions" do not require special attention. These are exceptions such as NullPointerException

    errors. You don't want to write code to cover possible errors . It is impossible to cover everything. However, if you want to catch an unchecked exception, you can.

  • Note. There are other objects with the ability to move . You can throw away all objects extending the class Throwable

    . But you shouldn't use this functionality in your code. This is valid for system errors and assertions.

If you have chosen a checked exception, then you need to warn the developers by specifying throws SomeException

in your method signature. For an unchecked exception, you don't.



The compiler checks this. It easily recognizes thrown exceptions because thrown exceptions extend the class RuntimeException

.

Finally, in your case, you've propagated the exception all the way to the top , to your method main()

. And even there you continued to spread it. The JVM will just print it in this case.

+2


source


I think the exception handling is very logical and throws are necessary because you don't always want to handle the exception at the level that might happen.

Let's say you have an object that manages values ​​that exist in multiple files

public class MultipleFileManager {
    private List<String> filesContent;
    .
    . 
    .
    public void addFileContent(String filename) {
        File file = new File(filename);
        try {
            FileReader fr = new FileReader(file);
            .
            //adding filecontent to filesContent list
            .
        } catch (IOException e) {
            System.err.println("file not added");
        }
    }
}

      



In this example, you obviously want to handle the exception at the MultipleFileManager level , because it would be very wrong if only one corrupted file could destroy the entire stream .

Therefore, the throws IOException statement in the methods of the FileReader class tells you that you will handle the exception at the addFileContent () level , or you risk compromising the entire thread in which the exception is thrown if you encounter a corrupted file.

And if it happens that this thread is the main thread, the whole application will crash.

+3


source


Allows you to diagnose:

First of all, you will throw an exception due to the code

throw new IOException();

      

When you put a try catch block, the exception will be handled in the catch block.

Remember that there are two types of Exceptions: Checked and Disabled Exceptions and then Errors .

The throw keyword just throws an exception (this could be a custom exception too !!!) and stops execution (unless a catch is used for the exception)

In general practice:

  • The catch block will handle the Exception
  • Throws will pass the error to its caller with no recovery mechanism.
+2


source







All Articles