• 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

doubt: K&B Page406 #11

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
11.
D. It is appropriate to use assertions to generate alerts when you reach code that should not be reachable.

The point is unreachable code can not even compile, how can use assert at that situation?



compile fail: unreachable code

Best,
Yue
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What do you think about the code above?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One question to consider is the difference between code that is Unreachable and code that should not be reachable.

Consider a method that converts and integer to a string value and is meant to only convert the values 1 and 2.



It is private, so whoever is writing the class has complete control over how it is called. Since the method only expects to be called with a value of 1 or 2 the default branch of the switch should not be reachable. However, as far as the compiler is concerned, the default statement is reachable, hence the method will compile. (ok, for those who are pedantic, it would need to be contained in a class that compiles)

We can put an assert into the default branch to catch the situation where someone makes a call to the method with a value we were not expecting. e.g.





Regards

Dave
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic