y2p
y2p

Reputation: 4941

Continue on exception

I get a few exceptions when I run my code. What I want to do is, I want to continue on FileNotFoundException and NullPointerException and break on any other exception.

How can I go about it? Thanks

Upvotes: 0

Views: 844

Answers (3)

Ash Khote
Ash Khote

Reputation: 1

multiple catch block to caught exception arised in try block

<code>
try{<br/>
// Code that may exception arise.<br/>
}catch(<exception-class1> <parameter1>){<br/>
//User code<br/>
}catch(<exception-class2> <parameter2>){<br/>
//User code<br/>
}catch(<exception-class3> <parameter3>){<br/>
//User code<br/>
}
</code>

Source : Tutorial Data - Exception handling

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120308

You can do this as @daniel suggested, but I have some additional thoughts.

  1. You never want to 'do nothing'. At least log the fact that there was an exception.
  2. catching NullPointerExceptions can be dangerous. They can come from anywhere, not just the code where you expect the exception from. If you catch and proceed, you might get unintended results if you don't tightly control the code between the try/catch block.

Upvotes: 0

Daniel
Daniel

Reputation: 28104

try {
    stuff()
} catch( NullPointerException e ) {
   // Do nothing... go on
} catch( FileNotFoundException e ) {
   // Do nothing... go on
} catch( Exception e ) {
   // Now.. handle it!
}

Upvotes: 5

Related Questions