question about try/catch

Discussion in 'Java' started by Mark, Oct 14, 2006.

  1. Mark

    Mark Guest

    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?
     
    Mark, Oct 14, 2006
    #1
    1. Advertising

  2. Mark

    Pawel Szulc Guest

    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
     
    Pawel Szulc, Oct 14, 2006
    #2
    1. Advertising

  3. Mark wrote:
    > 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.
     
    Brandon McCombs, Oct 14, 2006
    #3
  4. Mark

    Tim B Guest

    "Mark" <> wrote in message
    news:...
    > 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 )
    > {
    > 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
     
    Tim B, Oct 14, 2006
    #4
  5. Mark

    Mark Space Guest

    Tim B wrote:
    > "Mark" <> wrote in message
    > news:...
    >> 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 )
    >> {
    >> 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
    }
    }
     
    Mark Space, Oct 14, 2006
    #5
  6. 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

    http://opalpa.info/
     
    opalpa http://opalpa.info, Oct 14, 2006
    #6
  7. Mark

    jmcgill Guest

    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.
     
    jmcgill, Oct 15, 2006
    #7
  8. Mark

    Mark Guest

    Mark Space wrote:
    > Tim B wrote:
    > > "Mark" <> wrote in message
    > > news:...
    > >> 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 )
    > >> {
    > >> 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
     
    Mark, Oct 18, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    374
    Kevin Spencer
    Aug 7, 2003
  2. bienwell
    Replies:
    4
    Views:
    3,872
    bienwell
    May 27, 2005
  3. Matt
    Replies:
    1
    Views:
    414
    Michael Rauscher
    Jun 12, 2004
  4. Matt
    Replies:
    5
    Views:
    428
    Adam Maass
    Jun 28, 2004
  5. xuatla

    question on try/catch

    xuatla, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    1,957
    Andrew Koenig
    Mar 5, 2005
Loading...

Share This Page