question about try/catch

M

Mark

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?
 
P

Pawel Szulc

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
 
B

Brandon McCombs

Mark said:
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?

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.
 
T

Tim B

Mark said:
okay let's say i have something like

try {
myBooks.insert(new
Resource("c","ISBN","LCN","publisher","aerg","aergaerg",4,"2006-10-12",156))
;
Resource("d","ISBN2","LCN2","publisher2","dbdb","earg",4,"2006-10-11",179));
Resource("a","ISBN2","LCN2","publisher2","g4gaer","vearv",4,"2006-10-11",179
));
Resource("f","ISBN2","LCN2","publisher2","aergaerg34","aervrev",4,"2006-10-1
1",179));
Resource("q","ISBN2","LCN2","publisher2","far3w2","rfvaer",4,"2006-10-11",17
9));
}
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?

put your Resource objects in an array or a list and iterate over it, doing
the insert in the try/catch
 
M

Mark Space

Tim said:
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
}
}
 
O

opalpa opalpa

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);
} catch( ListException le ) {
System.out.println(""+le);
}

opalpa
(e-mail address removed)
http://opalpa.info/
 
J

jmcgill

Pawel said:
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.
 
M

Mark

Mark said:
Tim said:
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
}
}

i guess that's a good way of doing it :) thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top