• 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

Exception vs Compilation Fails

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is kicking my butt

I'm doing the Self-Test and I SAW the problem, there was a multi array being cast to an array.
But I wrote down the answer for Compilation Fails, the answer was Exception on that line.

How do I distinguish between a compile error and an exception error? I'm missing questions even though I see the error!
How do you know if the code will compile and throw an exception or not compile at all?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's difficult to give a simple rule. I suggest asking the question - "what does the compiler know?". If the cast could possibly work, then the compiler will allow it, and you'll get an Exception. But the compiler will step in if it can't possibly work.

Here's a simple example:
What the compiler won't do is try to deduce from the code exactly what the object type is. It just goes on the reference type.

If you have issues with a more complicated example, post it and we can try and explain it.
 
Greg Morphis
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


is the example from the book.. I saw the error, just figured the compiler would catch it but I guess since it gets cast as an Object and then tries to get cast back to an array the compiler can't follow? I just wish I had a "check" to know if it would cause a compiler error or exception.
Thanks for the help
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so my approach suggests asking "what does the compiler know about o1?"

The reference variable for o1 is Object. Which means it could contain anything - including an int[]. That's all you have to consider: the compiler doesn't look any further than that. The reference type tells it that the cast might work.
 
Greg Morphis
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So once you see the problem, then start breaking it down and tracking it down? I hope this book goes over that a little better than it has.
I'm only through Chapter 3.
Thanks for the information!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem.

Oh by the way, can you say which book it is? Content that's from somewhere else needs to be referenced here, and I've jusrt realised the book isn't actually mentioned in this thread. Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic