• 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: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is from Dan's Mock exams. I have changed the original code. I am confused with exceptions.(with "extends").

I have posted this in Java Intermediate also. My question is why
Example1 will not compile. Where as Example2 compiles. I am confused with this. Please explain.

=================================

Example 1
=================================



==============================================

Example 2
==============================================



Thansk in advance,
Mubeen.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very surprising to me too!
(for example 1, the compiler says "Unreachable catch block for Level3Exception. This exception is never thrown from the try statement body")

well, let's try to make things simpler: just remove the switch-block:

this time, both examples will fail compilation, as expected

so the switch-block is the answer to your problem:
a switch-block will be resolved only at runtime. that means that the compiler doesn't know what will happen with it at compilation time. all what the compiler knows, is that this switch clause can throw 3 different exceptions (Level1Exception, Level2Exception, Level3Exception).

in example 1, the compiler sees that all exceptions that could possibliy be thrown from the switch clause are catched at the line "catch (Level1Exception e) {b++;}". Therefore, he can be sure that the line "catch (Level3Exception e) { d++;}" will NEVER be reached. And as a brave compiler, he doesn't let the code compile.

in example 2, the compiler cannot be sure that all exceptions are catched at the line "catch (Level2Exception e) {b++;}". Indeed, the compiler thinks that the catch-block could throw a Level1Exception. Therefore, he lets the code compile.

If you still have trouble figuring out how the compile works, maybe it will help you if you replace x=3; by "x = new java.util.Random().nextInt(4);" which produces a random integer from 0 to 3. In this way, we don't know what the switch-block will do.

hth, renaud
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i got it that it is because of that Switch Run tim phenomenon, But how come compiler knows what is going to be called.....
Still in both cases error should not come,level 3 is sub class to level1 and level2 . so once it is not reachanbel and once it is reachable....
Kindly explain how it is happening.......
 
Renaud Richardet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i got it that it is because of that Switch Run tim phenomenon, But how come compiler knows what is going to be called.....


He doesn't know which of the 3 Exeption will be thrown. But he knows that in example 1, whatever Exeption is thrown will be catched at your first catch ("catch (Level1Exception e) {b++;}") and there's no need for a second catch ("catch (Level3Exception e) { d++;}")


Still in both cases error should not come,level 3 is sub class to level1 and level2 . so once it is not reachanbel and once it is reachable....


in example 2, the compiler thinks that the catch-block could throw a Level1Exception. Therefore, he lets you write a second catch clause (EVEN IF IT WILL NEVER BE REACHED, i guess compilers are silly sometimes ).
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Renaud,

Thanks for detail explanation. First thing i will not post same question in two forums in future.

---

Now in other place "Dave" said, e2 and e3 are at the same level. Because of that code compiled fine.

I think that is not correct answer for this. Please correct me if i am wrong. When we use "extends" it will like this.


Exception
----------
e1 (e1 extends Exception)
----------
e2 (e2 extends e1)
----------
e3 (e3 extends 2)
----------


Thanks,
Mubeen.
[ February 15, 2005: Message edited by: Mubeen Shaik ]
 
Renaud Richardet
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mubeen

Look at my post on the other forum. (Boy, it looks like multithreading posting )
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic