• Post Reply Bookmark Topic Watch Topic
  • New Topic

a question in the book head first java sharpen your pencil  RSS feed

 
changyu ji
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of these do you think might throw an exception that the complier would care about? We are only looking for the things that you can't control in your code.We did
the first one. ( The question is from the book Head First Java in page 325,sharpen your pencil part)

Things you want to do -------------------------------------------------------------------------------------------What might go wrong

_can't control__connect to a remote server ------------------------------------------------------------------- ___the server is down___
_can_________access an array beyond its lengyh ---------------------------------------------------------- __the byte is used by other programs, operating system don't allow you to access__
_____________display a window on the screen ------------------------------------------------------------- ________________________
_____________retrieve data from a database --------------------------------------------------------------- ________________________
_can't control__see if a text file is where you think it is ------------------------------------------------------ __You do not have permission to enter the directory_
_can_________create a new file --------------------------------------------------------------------------------- __Duplicate file names__
_can_________read a character from the command-line ----------------------------------------------------- _did not input anything_
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would appear to be the same question as you asked earlier. Please read this. I might be able to make your post easier to read with a bit of editing.
Can you really not think what might happen if you do the things in the questions? It is worth guessing and telling us your guess, because people here don’t simply give out such answers. But we will happily help if you give us something to work on.
 
changyu ji
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just can not think out what will happen when displaying a window on the screen,
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if the wire to the screen is disconnected?

Please don’t try changing the formatting of your post like that. I spent ages reformatting it yesterday so you could actually read it and your alterations have made it worse.
 
Amy Hardy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a newB and trying to answer this question as well. Here are the answers I came up with. Please tell me if I'm correct.

✅ Connect to a remote server - the server is down
- access an array beyond its length - fine
- display a window on the screen - fine
✅ Retrive data from a database - database unavailable
- see if a text file is where you think it is - fine
✅ Create a new file - server is gone to save file
- read a character from the command-line - fine
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I don't have my HFJ to hand, so please remind us what the question says. I don't think you have got them all right.
 
Amy Hardy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
Here is the question:

Which of these do you think might throw an exeception the compiler would care about? We're only looking for things that you can't control in your code. We did the first one.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a horrible method called read() which reads one character from the command line. Please read its details.
What sort of exception will you get if the text file isn't where you think it is?
 
Amy Hardy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So wouldn't the text file check be a Boolean runtime error not a checked exception?

Same with the read ()?
If it's reading a char from the command line, is that asking for user input? Would it be null if nothing was entered or would there need to be an exception?
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the documentation for creating a reader for a text file, or the read() method? While you are looking, see whether there is any chance of read() ever returning null.
 
Amy Hardy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where would I find the read () documentation?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exercise was given in the context of discussing the difference between checked and unchecked exceptions, right? I don't think you really need to worry too much about details of a particular class or method. The Java API is way too big to infer which, if any, specific method the authors had in mind when they included these items. Even I would hesitate at some of these because it really depends on the class and method you use in your code. I wouldn't agonize too much about being right or wrong on this exercise.

Just think of unchecked exceptions, also known as runtime exceptions, as exceptions that are usually thrown because of some programming mistake. It's impossible to write a compiler that can anticipate programming mistakes because the possibilities are limitless so those kind of exceptions are left unchecked. Things like trying to access an array with an index that is outside its bounds, casting an object to an incompatible type, trying to use a null reference to call a method, these are things the compiler doesn't check because the programmer can avoid them by changing the program's logic.

Checked exceptions, the ones that the compiler cares about, can occur even if the logic involved is perfect. Unchecked exceptions are usually associated with the environment that the program is operating within, like trying to access an external resource that is unavailable, like a file on disk or another computer on the network. These are common problems that are outside of the programmer's control and they can occur even when there is nothing wrong with the program logic itself.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amy Hardy wrote:Where would I find the read () documentation?
Try passing a text file to a FileReader object. Look at its constructors. Let's try the last constructor shown. That should enable you to read a text file.

For a single char, I would use System.in.read(). Actually I wouldn't use read(), because I think read() is a horrible low‑level method and there are much easier ways to read with higher‑level methods.

It takes a long time to learn to find your way around the API documentation; I think those links will help and I think they will give you information about kinds of Exceptions one might expect from those methods.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is what the authors had in mind with this exercise but here's the general approach I would take to figure some of these items out.

Let's take the one that says "see if a text file is where you think it is," for example.

First, I'd search for information on how to check if file is where I think it is in Java. I'd read through some of the search results and will find a number of code examples that use the File.exists() method.

Next, I will go to the File.exists() method's API documentation and read through it. I see that the method does not declare any exceptions with a throws clause in the header so that tells me the compiler won't care. However, I do see a link to SecurityException so I follow that. I would then see that SecurityException is a subclass of RuntimeException, so it's an unchecked exception. Again, the compiler won't care.

My conclusion then would be that if I used the File.exists() method to check if a file is where I think it is, the compiler would not care about checked exceptions related to that call because there isn't one involved.

NOTE: Going through a deductive process like that is certainly one approach you can take. Reading API documentation can be boring though. Another approach, of course, is by experimentation. That is actually more my style of learning. The proof is in the pudding, so I tend to lean towards making pudding. After finding examples that use File.exists(), I'd try those examples out or try to write my own program that uses it. I would see what happens if I don't wrap the call to File.exists() in a try-catch block or include a throws clause in my method header. If my example code doesn't get a compiler error, then I know there are no checked exceptions involved.
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know HFJ is getting a bit old now (still a good book, though) and the File class is regarded as legacy code, so I went to the Java™ Tutorials (AH: please bookmark that website) and found a page about checking files, and it suggests Files.exists(path). So let's try some code out:-Because I am using args, I had to pass the name of a file to the command line:-
$ java FileExistenceDemo /home/critchie/installationInstructions_History
false
$ java FileExistenceDemo /home/critchie/installationInstructions_history
true
Note the difference depending on whether I spelt the name right or not. If you read the tutorial, you will find there are three possible outcomes about file existence: exists, doesn't exist, and don't know. Let's change the code a bit: I looked for a lines() method and found this. That returns a Stream<String> iterating the entire file line by line. Have a close look at its documentation (most recent link underlined in blue). Look and see what it says about exceptions.
 
Amy Hardy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure.
What is your final version of the quiz?
 
Amy Hardy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

✅ Connect to a remote server - the server is down

- access an array beyond its length - fine

- display a window on the screen - fine

✅ Retrive data from a database - database unavailable

- see if a text file is where you think it is - fine

✅ Create a new file - server is gone to save file

✅ read a character from the command-line - user inputs nothing
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks right; I think I misunderstood the bit about text file as meaning you want to read the file. As soon as you do that you risk an IOException, which is checked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!