Java – is there any other throw role instead of propagating checked exceptions?

After more and more research on the throws statement in exception handling, I am confused I found –

class @R_474_2419@ing1{  
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 throw, there is still one exception - my console displays the following error –

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

Until I did not call METH1 in the try-catch block, although I used throws, I had an exception. What is the role thrown?

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 any other role to throw instead of propagating checkedexception?

Solution

As you said, there are two exceptions

>"Checking exceptions" requires the attention of developers When you use the method of throwing a checked exception (i.e. IOException), you need to process it (i.e. catch it) or propagate it to the caller (i.e. throw it), assuming that the caller will catch it. > "Unchecked exceptions" do not require special attention These are nullpointerexceptions, bugs and other exceptions You don't want to write code to cover potential errors Can't cover everything However, if you want to catch an unchecked exception, you can. > Note: there are other throwing objects You can throw all objects that extend the throwable class However, you should not use this feature in your code This is actually a system error and assertion

If you throw a checked exception, you need to warn the developer by specifying that someexception should be thrown in the method signature For unchecked exceptions, you do not need to

The compiler checks this It can easily identify unchecked exceptions because unchecked exceptions extend the runtimeException class

Finally, in your case, you propagate the exception all the way to the top () method Even there you continue to spread it In this case, the JVM will only print it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>