Results 1 to 9 of 9

Thread: try catch...

  1. #1
    MarkWilson is offline Member
    Join Date
    Jun 2008
    Location
    us
    Posts
    47
    Rep Power
    0

    Post try catch...

    if i am having
    1.try block with multiple catch statement , which one get executed
    and how?
    or
    2.in try catch block can i have multiple finally block

    try{
    }
    catch()
    catch()
    catch()

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, considering these last three questions, I am going to stop giving you any advice/help/info, as it appears as though you are doing homework, or taking a quiz.

  3. #3
    MarkWilson is offline Member
    Join Date
    Jun 2008
    Location
    us
    Posts
    47
    Rep Power
    0

    Default

    no my dear friend ,i m not
    c today is my 1st day in this forum
    so the question i remember that question i m just asking

    if u dont want to help me thn its ok ?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then visit this site and work your way through it.

    The Java™ Tutorials

  5. #5
    MarkWilson is offline Member
    Join Date
    Jun 2008
    Location
    us
    Posts
    47
    Rep Power
    0

    Default

    but tell me the answer of my question in short .
    thankx

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    1 The first one that applies, of course.
    2 Since a finally block is always executed, and is always executed last, what do you think?

  7. #7
    MarkWilson is offline Member
    Join Date
    Jun 2008
    Location
    us
    Posts
    47
    Rep Power
    0

    Default

    ok at first question i m asking
    which catch stmt execute first , u say 1st one
    but see if my code is
    try
    {
    .... // database conn. logic
    .
    ..}
    catch(IOException e)
    {
    }
    catch(SqlException e)
    {
    }
    in this if i got some error during sql connectivity so catch(sqlException e) get call , am i right and wht abt 2nd one
    this is my 1st Q?

    now in 2. if i right system.exit(1) in try blk thn aslo my finally blk get executed . or some condition is there

    plz reply

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Like I said, the first one that applies. Did you get an IOException or an SQLException?

    Well, think of the finally block as a door, you have to open it before you can leave, however, if you burn the house down while your still in it, will you still be able the door? Of course if you terminate the program it won't execute, as the program execution will never come to that point, since it will already have burned to the ground.

  9. #9
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    I would like to answer your questions..
    1.try block with multiple catch statement , which one get executed
    and how?
    try
    {
    .... // database conn. logic
    .
    ..}
    catch(IOException e)
    {
    }
    catch(SqlException e)
    {
    }
    catch(Exception e)
    {
    }
    When an exception is thrown it will check the 1st exception if the type doesnt match it will navigate to 2nd and so on untill it finds the correct type and if not found it will throw Exception and if it finds the correct type it wont check the other catch blocks.


    You cant have a multiple finally block.There will be only one finally block for a try block.

    if i right system.exit(1) in try blk thn aslo my finally blk get executed
    The answer for this would be NO. Finally block will get executed for everything even if the try block has a return stmt but if it has system.exit() finally block wont get executed as that stmt would cause jvm to shut down.
    To finish sooner, take your own time....
    Nivedithaaaa

Similar Threads

  1. Error with my Try Catch
    By kewlgeye in forum New To Java
    Replies: 9
    Last Post: 05-03-2008, 04:38 AM
  2. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 08:51 PM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 05:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 09:52 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •