• 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

Question on switch being unreachable

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below first question is from Dan's ,and it raises a compile time error,saying that switch is unreachable when nested try(second) does not execute.Please find another program below:



why doesn't this program raise the same error, when does the switch block gets unreachable.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasad,

your first Example of Dan's exam....

If you look at it, you will find that in the first try block, the first statement is "throw new Level1Exception();". so whenever that block runs, it throws and exception and it directly reaches to the corresponding catch block. So avoiding the new try block inside the try block. So this way, the second try block will never reach in any condition. This is why it throws an error while compilation.

your second example.........

You always reach inside the switch block, and execute one of the cases. Its completely a different issue!!!

The difference between these two conditions is that first is related to Exceptions and the other one is a condition statement.

I hope it helps
Kaps
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic