• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Subclassing OutputSream - Is my Scanner really closed?

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to connect an java.io.OutputStream to a subclass of javax.swing.JtextArea ( That is just what an author is doing in his book to illustrate how we subclass an OutputStream and this example is going to be refined in the later chapters ). Basically I'm trying to learn how the basic input/output works in Java and I possess no basic understanding of Swing components currently ( this is an area of programming that doesn't interest me much; however if the solution of the problem I'm getting is related to swing/graphics/awt stuff, I'm willing to try and learn some of the basic aspects of Swing/graphics/awt components.).

The code example ( everything but the main method ) I have posted is from the book Java IO, by Elliotte Rusty Harold. However the main method is my own addition to his fine example - hence that is also where I'm facing issue/issues.

Here is the code.


As you see, I have tried to create some basic JFrame and a JPanel so I can put a JTextArea within it. I have created an OutputStream and a Scanner inside a try with resources and I expect that my OutputStream and Scanner will automatically get closed once the user enters the String exit. I'm not sure that is happening cause I am able to keep typing to the console even after I enter 'exit' on my console and even after the program prints 'exiting'. And my program keeps running. It seems my program is waiting for me to close the JFrame cause when I close it, the program terminates if there are no other instructions pending to be executed ( if 'exiting' has been printed already ). So I am not sure if my Scanner object is really closed. I think after the Scanner is closed, I should not be able to write anything on the console. Would that be right?
Also I'm also not sure if I have coded the try with resources correctly - specifically the part that should give me the suppressed Exceptions. Is there a better way to get the exceptions that a try with resources may suppress?

Thanks,
Chan.

Note : I understand that my main method is too long. Partly it is because I'm creating the UI in the main method. I should probably have a method that does it. However I've ignored that bit for now, just so almost no knowledge of JFrame/JPanel/JTextArea can suffice to test the creation of an OutputStream that can be appended to a component (english ) and I can concentrate on the IO part for now.



 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote: So I am not sure if my Scanner object is really closed. I think after the Scanner is closed, I should not be able to write anything on the console. Would that be right?



I think I've figured this bit. I'm being able to write to the Standard input ( System.in) cause I can have access to my console for as long as my program is running. If the standard input was a file, I'd have access to it even after my program terminated. A Scanner is just a means that lets a program scan input from an input source which in my case was the standard input ( the console - the special case where I'd have access to it only for as long as my program was running and while my program was not using the Console - as a standard output/error destination -to type something to it ). So yes it seems the Scanner was closed cause I was able to write to the the System.in but the scanning of this input wasn't happening.

So now the part that is pending is the try with resources. Is my way the right way to code a try with resources statement?

Thanks,
Chan.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are confusing your streams, the output stream and Scanner object are closed but System.in (which echo's its input to the console) is not closed.

To close the frame call setVisible(false) and dispose() on it.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Tony. That really helped.

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:
So now the part that is pending is the try with resources. Is my way the right way to code a try with resources statement?



Just the above is pending. What should be the right way to get a catch block to print the suppressed exceptions? Do you see a problem/many problems with the way I have coded it?

Thanks for your help.
Chan.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've not used suppressed exceptions so this could be a case of the blind leading the blind. However, the suppressed exceptions section of the tutorial http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html says "You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block."

You are creating a new Throwable and calling getSuppressed on it which is not what the tutorial says to do.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I had referred to that page -- it seems either the page is missing on some bit of an information or most likely I need some more help/practice ( whatever I can get ) to understand it. I tried the Throwable.getSuppressed() way but the getSuppressed method isn't static.

One other thing that is confusing is this.

Let us say, I code a try with resources like this --



Now let us say there are two exceptions ( let us ay one in read and one in close ) but one of it ( the exception thrown while closing will get suppressed ) gets suppressed. In such cases where do we type the suppressed exception handler considering it is already suppressed. I think my construction of wrapping the try with resources into another try catch is futile cause the exception is suppressed anyway so it wont be caught. So where do we code it? In a finally block associated with the first try with resources and we do not code an outer try? Or do we code it within a finally block of the outer try and just do the logging of this exception in the finally block ( but doing this kind of processing in finally doesn't seem like the right way, or is it? ). So I'm not sure how we can use the suppressed exception feature really.

Also is it right to assume that in most cases there never really is a need to know if there were exceptions that got suppressed?

Thanks,
Chan.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually I had referred to that page -- it seems either the page is missing on some bit of an information or most likely I need some more help/practice ( whatever I can get ) to understand it. I tried the Throwable.getSuppressed() way but the getSuppressed method isn't static.


When it says "by calling the Throwable.getSuppressed method" it doesn't mean create another Throwable object or use a static method of the Throwable class it means call the getSuppressed method on the object that is thrown ie in your case the Exception e in the try-with-resource statement. Remember all objects that are thrown must extend Throwable.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

Actually I had referred to that page -- it seems either the page is missing on some bit of an information or most likely I need some more help/practice ( whatever I can get ) to understand it. I tried the Throwable.getSuppressed() way but the getSuppressed method isn't static.


When it says "by calling the Throwable.getSuppressed method" it doesn't mean create another Throwable object or use a static method of the Throwable class it means call the getSuppressed method on the object that is thrown ie in your case the Exception e in the try-with-resource statement. Remember all objects that are thrown must extend Throwable.



Thanks, Tony. So that means it can only go in the catch block then? Sorry it seems I am shooting too many questions -- actually it's more like I'm adding up all your suggestions and what I'm reading in forums and elsewhere to build a complete thing from bits and pieces.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also is it right to assume that in most cases there never really is a need to know if there were exceptions that got suppressed?


In most cases the first exception that is thrown is probably the most important but it may be that you want to ensure your object close doesn't throw any exceptions so you know you have left your object in a safe state, in which case you may well need to check the suppressed exceptions.

I really depends on what you are doing. When working with streams there's not much you can do if close() throws an exception but if you write your own class that implements Closeable there may well be some other action you can/should take.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:
Thanks, Tony. So that means it can only go in the catch block then?


No, it means you can only call getSuppressed on the exception that is caught by the catch block associated with try-with-resources statement. You can pass that exception to other methods which can call getSuppressed or re-throw it and have some other method catch it and call getSuppressed.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Tony. All that makes it so much clear to me now.

Chan.

Edit - I just realized how stupid my try with resources question is. And how stupid what I'm doing in that bit of code is. I messed all static ( there is no reason to actually have such a static method. Even if such exceptions were logged in a static datastructure what would be the key for a static method to retrieve them from that structure for a specific case ) and instance ( heck I created a new Throwable - implies I thought the exceptions were logged in a static datastructure and any instance can get them - even more weird ) concepts. Trust me I know those basics. :-) It's just a thing I forgot temporarily ( don't know how ), till Tony helped me see it.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic