• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

OCA 8 p. 331 and 351 question 19 Boyarsky and Selikoff  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's because it's the second to the last question in the book and my eyes are blurry, but the answer on # 19 reads, "Option B is not allowed because the method called inside the try block doesn't declare an IOException to be thrown." But what method is called inside the try block? All I see is a println(). Is that what they are referring to? It's question 19 on p. 331. How is the IOException an unreachable catch block? Totally confused. Sorry if I'm missing the obvious.

m.
 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I suppose it would help if I typed in the code.

 
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Kevin wrote:. . . But what method is called inside the try block? All I see is a println(). . . .

Yes, the method called is println(String). That doesn't declare that it might throw any checked exceptions. The compiler knows that IOException is a checked exception, so it tests whether there is any code in the try that might throw such an exception. Since nothing does declare IOException, it knows that the first catch is unreachable. Remember that any code possibly throwing IOException must declare it.
The second catch uses runtime exception, which is unchecked, so it is possible that the preceding code might throw a null pointer exception or some other unchecked Exception and the compiler wouldn&apost be aware of that, so it has to give the code the benefit of the doubt and assume it might be reachable.
 
Mark Kevin
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so for the exam I'm expected to know that println(String) doesn't throw IOException. That's pretty granular. So you can't catch a checked exception unless there is code in the try block which might throw that checked exception, which includes a call to a method that might throw a checked exception. The reason is that the catch is unreachable, it would never get used. And declaring throws IOException in main wouldn't make a difference. Thanks. This clears up my confusion about unreachable code.
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Kevin wrote:. . . I'm expected to know that println(String) doesn't throw IOException. That's pretty granular. . . . .

But you use println() every few minutes, so you should be familiar with its properties. You should also know what happens if you call binarySearch() on an unsorted array/list.
 
Mark Kevin
Ranch Hand
Posts: 67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should also know what happens if you call binarySearch() on an unsorted array/list.



It produces an unpredictable result, as per the book. Thanks.

Mark
 
author & internet detective
Posts: 39128
723
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
For the OCA, you need to know that *no* methods throw an IOException unless they explicitly tell you otherwise.

This gets harder on the OCP.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!