Results 1 to 4 of 4
Thread: [SOLVED] multiple try catch
- 04-01-2009, 08:19 PM #1
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
[SOLVED] multiple try catch
Is there any difference between these two code fragments?
Do I need a try statement for each exception?
Java Code:try { try { //Code to be executed } catch (NumberFormatException e) { System.out.println("Error: Invalid parameters."); System.out.println(e); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error: Invalid parameters."); System.out.println(e); }
Java Code:try { //Code to be executed } catch (Exception e) { System.out.println("Error: Invalid parameters."); System.out.println(e); }
- 04-01-2009, 08:28 PM #2
You can have multiple catch for each try block.
Java Code:try { //Code to be executed } catch (NumberFormatException e) { //Handle error } catch (ArrayIndexOutOfBoundsException e) { //Handle error }
- 04-01-2009, 08:34 PM #3
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 13
Ooooh... Thanks! : )
- 04-01-2009, 09:12 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
in some way both work fine but the 1st one is more specific exception that u want to caught. the second one where u use exception which is a supper class of all exception can catch all. so can be a problem in debugging . but in the first example one need to make sure that all the code are reachable by compiler.
Similar Threads
-
how to catch two exceptions in one catch()?
By arnab321 in forum New To JavaReplies: 1Last Post: 11-06-2008, 10:54 AM -
try catch...
By MarkWilson in forum New To JavaReplies: 8Last Post: 06-27-2008, 05:39 PM -
Try Catch
By Renegade85 in forum New To JavaReplies: 4Last Post: 12-03-2007, 04:10 PM -
when to use try...catch
By javaplus in forum New To JavaReplies: 2Last Post: 11-18-2007, 08:52 PM -
Use try and catch
By zoe in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:50 PM
Bookmarks