Results 1 to 15 of 15

Thread: Try---finally

  1. #1
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default Try---finally

    Can Any One Suggest When We Need To Go For A Try-finally Instead Of Try-Catch-Finally

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you no need to catch exceptions, then use try-finally. try may be a block of code with multiple exit points. And the finally is another block of code that always executed when the try block is exit.

  3. #3
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Thanks Eranga. My Doubt Is If There Is No Need Of Handling Exception,

    Why We Need To Put It In Try Block?. The CleanUp Code We Can Put

    Inside finalize() block.?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, if you don't want to get the exceptions no need to add in a try block. But in some cases, such as working on an IDE, it automatically detect the exceptions and ask to include in try block.

    Actually handling exceptions is really important. Are you try avoid those.

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    and you will also need to put your code in Try block for Checked Exception, like IO,DateFormat etc.

  6. #6
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Eranga Sorry To Ask Again, But My Point Is Why We Need Some Code

    Which Is Expected To Be Handled, Using Any Of The Exception Handling

    Mechanism, ie try/catch, but need not be handled. If An Exception Is

    Expected, then shouldn't it be handled?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not clear what you ask.

    Are you looking another way to handle exceptions, rather than try/catch?

  8. #8
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    No. My Question Is Instead Of Try/Catch/finally When We Go For

    Try/Finally We Are Not Handling The Exceptions, Right?.

    So Here Is My Point. My Point Is That, If dont want to handle exception,

    Why We need to go for try block?

  9. #9
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Ok do not use Try Block if you dont want handle Exceptions but you can not use finally block without Try.

  10. #10
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Sanjeev, then we can go for finalize() also to do the cleanup code right?

  11. #11
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Yes you are right,
    finally block is only for cleanup with try block.


    sanjeev

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  13. #13
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Thanks Eranga. I will explore more on this, and will come back. Thank you very much

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Sure, anytime. And also you can throws exceptions if you want. In that case you no need to worried about try/catch also.

    As you said explorer something by yourself. You may come across with something interesting.

  15. #15
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

Posting Permissions

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