Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Exceptions in Runtime

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

I want to make my self clear with the catching exceptions. As all know that whether the exception is matched or not finally block will execute. So if there is any exception occurs in the finally block what will happen.I am thinking the application will crash.Is it true?

Thanks,
Raghu
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, In practice we put one more try catch inside finally..Here is an example for you clarification, following code read a file:
 
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it is better to put the finally inside a try.That way you only need one set of catch blocks.
 
Campbell Ritchie
Marshal
Posts: 79637
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is usually a bad idea to put anything which might create an Exception in a "finally," because it would obscure a real Exception arising from the previous try.
Whether the application crashes depends on whether the Exception is caught anywhere else. But look at this scenario
  • 1: Exception occurs in "try."
  • 2: Exception caught, and chained with another Exception: catch(FooException fe){throw new BaaException(fe);}
  • 3: Exception occurs in "finally."
  • 4: The Exception in the "finally" obscures the BaaException and the information about THAT error is lost.
  • 5: Exception from "finally" is caught elsewhere, application runs.
  • 5�: Exception from "finally" isn't caught, thread crashes, but nobody know there was another exception.
  •  
    Ranch Hand
    Posts: 1970
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Ram Adsumalli:
    I am thinking the application will crash



    What do you mean by "crash"?

    A Pure Java application should never crash, in the sense of the word that applies to something like a C or C++ program. A Pure Java program cannot do illegal memory access, for instance.

    The worst thing that can happen in a Pure Java application is that a thread throws an Exception or Error that is not caught by any handler, causing that thread to exit. If it is the only thread in the application, then the application will shut down. Whether you call that a crash is debatable.

    Note

    A Java program can crash if there is a bug in the JVM, or if there is a bug in some native code (JNI) that your, or third-party, code is using. Of course, a program with such native code is not a Pure Java application.
     
    Deepak Chopra
    Ranch Hand
    Posts: 433
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Campbell,

    you have given very strong point and it makes a lot of sense to me.i have a little confusion, it is always said that " Keep the clean up code in the finally" so If you see the example i have given above, I am doing the same. I need to close the resource before quitting the block. I am scared if an Exception occur while reading it will directly jump to Catch block. Here I am catching that Exception and will log out that Exception. But the resource is still open and i need to close it..So I wrote that code in the finally..!! but again i am scared if something happens while closing the stream so i put one more try catch inside that!! I seems to be completely agree with me..but the point you have given is also valid. So that situation what should be the correct implementation scheme.
    I think if after Catching the Exception if we are not re throwing it then its good to put try catch in the finally..and In case if we are throwing then we should not put try catch..But again if suppose in my case while reading the file i got an exception, Exception caught and i re throw it ..!! now
    i need to close the stream ?? - How to do that..?
    what if an exception occur while closing the stream ? how to handle that exception ?
     
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can use a seperate try/catch block to close the stream but not within a finally block.
    [ February 19, 2008: Message edited by: Arivazhagan Arutchelvam ]
     
    Campbell Ritchie
    Marshal
    Posts: 79637
    380
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No, you can have a try-catch inside a "finally," as Sunny Jain has shown earlier.

    Sunny, thank you. The advantage of what I showed you (which I didn't work out for myself, only I can't remember where I first saw it) is that you have all the Exception handling in the same place. You are treating the open Reader->read->close Reader sequence as a single operation.
    And if you want to throw something, you can do it like thisAssuming the ABCException has a suitable constructor, that will not only throw a new Exception, but will also chain the old Exception to it.
    You will notice that I did have the close() call in a finally, only it was inside a try.

    And what happens if there is an Exception whilst closing the Reader? Don't know. I think it is one of those unpredictable things about computing.

    Anybody else know?
     
    Ram Adsumalli
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Guys

    Thanks for your replies. Campbell, As i am new to java Just i am trying to explain your question. Could you please let me know is that resonable. what happens if we catch with the Generic Exception Block. So that what ever be the exception, that will be catch by the Exception class.



    Thanks
     
    Campbell Ritchie
    Marshal
    Posts: 79637
    380
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The problem with catch(Exception e) is that different Exceptions might require different responses, which discrimination is lost by using Exception.

    Also it will catch RuntimeExceptions like ArrayIndexOutOfBoundException and may lose them, whereas the correct response to such an Exception is to go back and correct the code.
    reply
      Bookmark Topic Watch Topic
    • New Topic