• 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

Exceptions...

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOURCE:www.danchisholm.net

CODE:1....






CODE2:



In the first code it is not showing any error eventhough we mention catch blocks for every exception..

but in the second code it is showing an error as exception never thrown in try block...

i know that if we are catching a checked exception which is not thrown in try block then it shows an error..

how can i find in the above code1 and code 2?






[ December 08, 2008: Message edited by: Ganeshkumar cheekati ]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kinda erro you're gettin in the first one chap? 'cause I'm not gettin any error in first one.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in code 1, each checked exception is being thrown inside the switch. But in the second case BlueException is not thrown in the body of try so it showing you an error...
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by GaneshKumar:

i know that if we are catching a checked exception which is not thrown in try block then it shows an error..



You've answered your question above. Just look carefully that you are not throwing the checked Exception BlueException in your try block in CODE 2 whereas you are throwing all the checked Exceptions in the try block and have a corresponding catch block in CODE 1.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In code:2 it is not throwing exception thats why it is showing an error..

it is cleared for me.

CODE:1


In code:1 the value of x is 2.
so it is throwing level2 exception which is handled by corresponding catch block.

what about remaining catch blocks?

Level3,Level2 and Level1 exceptions are checked exceptions...

here we have 2 catch blocks for Level1 and Level3 exceptions which are not throwing any exceptions..

why these are not showing any compilation error?

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler doesn't know at compile time that the value of x is 2. What it knows that the switch construct is throwing some exceptions and you have to catch it. The compiler cannot figure it out that only case 2 will be executed. Whenever a variable is involved, the compiler doesn't checks the value of the variable. Eg



Now here the compiler doesn't know that hello will never be displayed...
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If x would have been a compile time constant,say static final int x, then there would have been a Compiler Error, right?
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it...

compiler checks that whether 3 exceptions are thrown for 3 corresponding catch blocks or not...

thats y it compiles fine..

am i right?

:roll:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic