Java Forum / General / October 2006
question about try/catch
Mark - 14 Oct 2006 00:03 GMT okay let's say i have something like
try { myBooks.insert(new Resource("c","ISBN","LCN","publisher","aerg","aergaerg",4,"2006-10-12",156)); myBooks.insert(new Resource("d","ISBN2","LCN2","publisher2","dbdb","earg",4,"2006-10-11",179)); myBooks.insert(5); myBooks.insert(new Resource("a","ISBN2","LCN2","publisher2","g4gaer","vearv",4,"2006-10-11",179)); myBooks.insert(new Resource("f","ISBN2","LCN2","publisher2","aergaerg34","aervrev",4,"2006-10-11",179)); myBooks.insert(new Resource("q","ISBN2","LCN2","publisher2","far3w2","rfvaer",4,"2006-10-11",179)); } catch( ListException le ) { System.out.println(le); }
but insert(5) throws an exception. so it prints a nice little error message.. but then the rest of the inserts are skipped!
how could i get it to print the error, and then continue the rest of the inserts, throwing exceptions as necessary...without putting a try catch around each statement? is it possible?
Pawel Szulc - 14 Oct 2006 00:29 GMT since error was caugth you can think of all operations in the try like they did not ever happend. thats the point of the syntax - u trying something it aint working means you should no tdone it
jmcgill - 15 Oct 2006 03:45 GMT > since error was caugth you can think of all operations in the try like > they did not ever happend. Be careful. Just because an exception is caught in a try/catch block does not mean things within that block did not execute. Consider a try block that opens a DB prepared statement or some other resource that must be closed. You had better check those things in the finally block.
Brandon McCombs - 14 Oct 2006 00:45 GMT > okay let's say i have something like > [quoted text clipped - 22 lines] > the inserts, throwing exceptions as necessary...without putting a try > catch around each statement? is it possible? As Pawel stated, the definition of an exception is that you stop doing whatever you were doing because you ran into an exception case that wasn't unexpected and needs special handling. You should be putting a try/catch around each insert() separately. That's how I do things despite the fact it makes the code look a little ugly but when you want the code to continue on with other similar statements because they are independent then you have to do it that way. Even using the 'finally' part of a try/catch wouldn't help you in this case because you wouldn't know how many of your statements need to go in that block.
You could try to put all those insert() statements in a separately method and have the method throw the ListException but that still doesn't mean the others will continue; the new method would still stop processing because the exception was thrown.
That's everything I know of but others may have more advanced ideas.
Tim B - 14 Oct 2006 03:03 GMT > okay let's say i have something like > > try { > myBooks.insert(new Resource("c","ISBN","LCN","publisher","aerg","aergaerg",4,"2006-10-12",156)) ;
> myBooks.insert(new Resource("d","ISBN2","LCN2","publisher2","dbdb","earg",4,"2006-10-11",179));
> myBooks.insert(5); > myBooks.insert(new Resource("a","ISBN2","LCN2","publisher2","g4gaer","vearv",4,"2006-10-11",179 ));
> myBooks.insert(new Resource("f","ISBN2","LCN2","publisher2","aergaerg34","aervrev",4,"2006-10-1 1",179));
> myBooks.insert(new Resource("q","ISBN2","LCN2","publisher2","far3w2","rfvaer",4,"2006-10-11",17 9));
> } > catch( ListException le ) [quoted text clipped - 8 lines] > the inserts, throwing exceptions as necessary...without putting a try > catch around each statement? is it possible? put your Resource objects in an array or a list and iterate over it, doing the insert in the try/catch
Mark Space - 14 Oct 2006 03:35 GMT >> okay let's say i have something like >> [quoted text clipped - 34 lines] > put your Resource objects in an array or a list and iterate over it, doing > the insert in the try/catch Yeah.
Basically, and in psuedo-code:
String myData [] = "c", "ISBN", "LCN", "publisher", "aerg","aergaerg",4,"2006-10-12",156 // etc... all the data here
foreach( C in myData ) { try ( insert (C); // somewhere... } catch Error (e) { println ( e ); // barf up a hair ball } }
Mark - 18 Oct 2006 02:00 GMT > >> okay let's say i have something like > >> [quoted text clipped - 54 lines] > } > } i guess that's a good way of doing it :) thank you
opalpa opalpa@gmail.com http://opalpa.info - 14 Oct 2006 03:38 GMT like so:
Object l[] = { new Resource("c","ISBN","LCN","publisher","aerg","aergaerg",4,"2006-10-12",156), new Resource("d","ISBN2","LCN2","publisher2","dbdb","earg",4,"2006-10-11",179), 5 new Resource("a","ISBN2","LCN2","publisher2","g4gaer","vearv",4,"2006-10-11",179), new Resource("f","ISBN2","LCN2","publisher2","aergaerg34","aervrev",4,"2006-10-11",179), new Resource("q","ISBN2","LCN2","publisher2","far3w2","rfvaer",4,"2006-10-11",179) }; for (int i=0; i<l.length; i++) try { insert(l[i]); } catch( ListException le ) { System.out.println(""+le); }
opalpa opalpa@gmail.com http://opalpa.info/
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|