Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   question about try/catch (http://www.velocityreviews.com/forums/t387786-question-about-try-catch.html)

Mark 10-13-2006 11:03 PM

question about try/catch
 
okay let's say i have something like

try {
myBooks.insert(new
Resource("c","ISBN","LCN","publisher","aerg","aerg aerg",4,"2006-10-12",156));
myBooks.insert(new
Resource("d","ISBN2","LCN2","publisher2","dbdb","e arg",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","aergaerg 34","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 10-13-2006 11:29 PM

Re: question about try/catch
 
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


Brandon McCombs 10-13-2006 11:46 PM

Re: question about try/catch
 
Mark wrote:
> okay let's say i have something like
>
> try {
> myBooks.insert(new
> Resource("c","ISBN","LCN","publisher","aerg","aerg aerg",4,"2006-10-12",156));
> myBooks.insert(new
> Resource("d","ISBN2","LCN2","publisher2","dbdb","e arg",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","aergaerg 34","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.

Tim B 10-14-2006 02:03 AM

Re: question about try/catch
 

"Mark" <mnbayazit@gmail.com> wrote in message
news:1160780586.162553.76350@m73g2000cwd.googlegro ups.com...
> okay let's say i have something like
>
> try {
> myBooks.insert(new
>

Resource("c","ISBN","LCN","publisher","aerg","aerg aerg",4,"2006-10-12",156))
;
> myBooks.insert(new
>

Resource("d","ISBN2","LCN2","publisher2","dbdb","e arg",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","aergaerg 34","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 )
> {
> 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



Mark Space 10-14-2006 02:35 AM

Re: question about try/catch
 
Tim B wrote:
> "Mark" <mnbayazit@gmail.com> wrote in message
> news:1160780586.162553.76350@m73g2000cwd.googlegro ups.com...
>> okay let's say i have something like
>>
>> try {
>> myBooks.insert(new
>>

> Resource("c","ISBN","LCN","publisher","aerg","aerg aerg",4,"2006-10-12",156))
> ;
>> myBooks.insert(new
>>

> Resource("d","ISBN2","LCN2","publisher2","dbdb","e arg",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","aergaerg 34","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 )
>> {
>> 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
>
>


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
}
}

opalpa opalpa@gmail.com http://opalpa.info 10-14-2006 02:38 AM

Re: question about try/catch
 
like so:

Object l[] = {
new
Resource("c","ISBN","LCN","publisher","aerg","aerg aerg",4,"2006-10-12",156),
new
Resource("d","ISBN2","LCN2","publisher2","dbdb","e arg",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","aergaerg 34","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/


jmcgill 10-15-2006 02:45 AM

Re: question about try/catch
 
Pawel Szulc wrote:
> 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.

Mark 10-18-2006 01:00 AM

Re: question about try/catch
 

Mark Space wrote:
> Tim B wrote:
> > "Mark" <mnbayazit@gmail.com> wrote in message
> > news:1160780586.162553.76350@m73g2000cwd.googlegro ups.com...
> >> okay let's say i have something like
> >>
> >> try {
> >> myBooks.insert(new
> >>

> > Resource("c","ISBN","LCN","publisher","aerg","aerg aerg",4,"2006-10-12",156))
> > ;
> >> myBooks.insert(new
> >>

> > Resource("d","ISBN2","LCN2","publisher2","dbdb","e arg",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","aergaerg 34","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 )
> >> {
> >> 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
> >
> >

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



All times are GMT. The time now is 09:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.