Java exception handling

class chain_exceptions{ public static void main(String args[]){ try { f1(); } catch(IndexOutOfBoundsException e) { System.out.println("A"); throw new NullPointerException(); //Line 0 } catch(NullPointerException e) //Line 1 { System.out.println("B"); return; } catch (Exception e) { System.out.println("C"); } finally { System.out.println("D"); } System.out.println("E"); } static void f1(){ System.out.println("Start..."); throw new IndexOutOfBoundsException( "parameter" ); } } 

I was expecting line 1 to catch the NullPointerException from line 0, but that won't happen.

But why so ?.

When there is another catch block, why can't I handle the NPE handler in Line1?

Is this because throw jumps directly to the main () method?

+4
source share
11 answers
Blocks

Catch{ . . . } Catch{ . . . } are associated with try{ . . . } blocks try{ . . . } try{ . . . } try{ . . . } . The catch can catch exceptions that are thrown only from the try block . The other catch blocks after the first catch block are not associated with the try block, and therefore, when you throw an exception, they don't get caught. Or main() does not use exceptions.

The look of this for each catch block will do what you are trying to do.

 try{ try { f1(); } catch(IndexOutOfBoundsException e) { System.out.println("A"); throw new NullPointerException(); //Line 0 } } catch(NullPointerException e) //Line 1 { System.out.println("B"); return; } 
+8
source

The catch blocks are for the try block only. They will not catch exceptions from other catch blocks.

+3
source
Operators

catch only allow exception exceptions from the try { ... } block.

NullPointerException from the catch { ... } , not the try { ... } block try { ... } .

To catch the exception thrown from the catch , you need to add another try block to it. Outside, wrapping around the original try...catch will also work.

+3
source

The second catch does not get an exception from the first catch . You must add another try-catch inside the first catch block (or around the whole try-catch that you already have) to make this run as expected.

+2
source

Since java 7 you can use the code below or another option is to embed try catch commands, another option in java

 try { ... } catch( indexoutofboundsexception| nullpointerexception ex ) { logger.log(ex); throw ex; } 
+2
source

In your catch there are only exception exceptions thrown by f1() . They do not throw exceptions thrown into other catch the same try-catch-finally construct.

+1
source

Because f1() throws an IndexOutOfBoundsException .

 try { f1(); //throws IndexOutOfBoundsException } catch(IndexOutOfBoundsException e) //gets caught here immediately and does not check other catch blocks { System.out.println("A"); throw new NullPointerException(); //Line 0 } 
+1
source

Short answer: yes, throw will directly throw exception to the main method.

Typically, when a catch block is executed, it behaves like an else if , that is, it does not consider other alternatives.

+1
source

No, the reason it is not caught is because it is not in the try block that is associated with the catch block. If you want to catch this exception as well, you will have to flip the throw into a new try / catch group. The reason you would like to do this is a mystery to me.

What you can do btw also:

 catch (IndexOutOfBoundsException|NullPointerException e) 

It will also allow you to use the same catch block for several types of exceptions.

+1
source

Your expectation was incorrect:

The catch blocks are associated with the try block. Therefore, when an exception is thrown inside try , it leaves this area. You are now outside the scope of try , which means that you are no longer in the try / catch frame. Any exceptions thrown here (when you are not leaving) will not be caught by anything, and yes, the bubble from main .

+1
source

You cannot exclude catch from another catch , because you probably need to do something like this in your first catch block

 System.out.println("A"); try{ throw new NullPointerException(); //Line 0 } catch(NullPointerException e) //Line 1 { System.out.println("B"); return; } 
+1
source

Source: https://habr.com/ru/post/1500462/


All Articles