• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is while(true) good style?  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have a general design question. It is not uncommon to see code like this:



Now I would like to hear some opinions from experienced programmers. Isn't that very ugly style? I remember seeing code like this as an example of abusing exceptions. But maybe I'm wrong...

The reason I am concerned about this, is that the name "Exception" indicates something unusual, something which is not the rule. But using code like this explicitly relies on the exception to happen for the program to terminate at all.

So please share your opinions for I am trying to write a program using I/O and the only working version so far uses code similar to the one above. (which makes me a bit uncomfortable)

Thank you!
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, it's kinda ugly to use while (true) since you know the EOFException will be thrown anyway.

However, there are times when you need while (true), for example when you're writing a simple server so that the server keeps running. Although, there may be better way, that approach is commonly used.
 
Christoph Czon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply!

I will post an excerpt of my code, so you can help me find a way around the while(true).



How can I solve this more elegantly?
Basically I want to save any file from the web and store it on my Computer. That's it.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that DataInputStream In that case, using while (true) is probably what you need to use since the API says EOFException marks the end of stream.
 
Christoph Czon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that sounds reasonable. Also, unlike most other read() methods, readUnsignedByte doesn't return -1 or something when there's nothing left to be read. So in this case that's probably the only way to go.
Well, thank you for helping!
 
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, ok, DataInputStream and DataOutputStream are not the best classes to use in this situation. These classes are used to read/write primitive data types like ints and doubles. If you just want to read/write the bytes in a file, don't bother wrapping your input/output streams in the Data*Stream classes.

 
Christoph Czon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks, that is some neat code! I guess the reason why I initially picked the Data*Stream classes was because I saw the readUnsignedByte() method and writeByte() methods.. But that was more wrapping than necessary
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christoph Czon wrote: . . . that is some neat code! . . .
No, it isn't. you should have two nested try blocks with the finally after the inner try. That way you won't have to declare an Exception. Also you need one finally per object to close; that way an Exception in the first close() call won't affect the second close().
Also: avoid catching or declaring "Exception" as far as possible
 
Christoph Czon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should have two nested try blocks with the finally after the inner try.

I'm not sure if I understand that right. I need 1 try block for creating the Input and Output Streams. Then I need at least 1 more for reading from/writing to the according streams. How many try blocks does that make in total: 2 or 3?
Campbell Ritchie wrote:That way you won't have to declare an Exception.

You mean the one that main() throws? I just left it in because I didn't want to make the code too long and my initial question was about something else.
//EDIT: Oh I just saw that close() also throws an exception. I guess that's what you meant by declaring an exception..? Ok, I think I understand a bit better now, unfortunately I have to go now.. I will rewrite the code later. Still appreciate feedback Thanks!
Campbell Ritchie wrote:Also you need one finally per object to close; that way an Exception in the first close() call won't affect the second close().

Umm, I don't really know how to implement that. I tried to rewrite the code considering your suggestions. Please tell me what you think of it!
Here's the new version:
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christoph Czon wrote:
Campbell Ritchie wrote:You should have two nested try blocks with the finally after the inner try.

I'm not sure if I understand that right. I need 1 try block for creating the Input and Output Streams. Then I need at least 1 more for reading from/writing to the according streams. How many try blocks does that make in total: 2 or 3?

Campbell meant this:
If you want to catch any IOException, you add a try-catch block around this. The main structure then is:
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . Campbell meant this:
Exactly
 
Christoph Czon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh great, I got it now. All exceptions are caught, all streams are closed
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done. Note that Rob omitted the outermost try-catch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!