This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why does this compile and not throw an exception until run-time?

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code in the appropriate context, why does it compile and not throw a checked exception?
public synchronized void makeAppointment() {

it will compile but then throw an illegal argument exception at runtime because -1000 is an illegal argument. But the compiler can see that there is a -1000 in there right now, why does it still allow it to compile?
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mui,
IllegalArgumentException is a runtime exception, so it was checked at run time.
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what i meant is wait(-#) is not a valid call, because wait can't take a negative number. I'm just wondering why the compiler would let this compile. It is like me calling a function
test(double d) when only test(int i) exists. I mean the compiler can see that during compilation, how does one determine when an error should be caught at compile-time or run-time, other than memorizing all errors. The reason I ask is because, usually there is some logic in determining these things, so if you understand the logic you can usually not have to memorize everything. As that is the case for me this time. It just doesn't make sense to me, but probably only because I don't understand the logic, so if somebody can explain that I would greatly appreciate it. Thanks.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mui,
In human-sense, wait(-1000) is definitely not a valid call,
however, compiler allow this because -1000 is a
valid parameter to wait(long); (-1000 certainly fit as a long). so it does not complain.
-> try wait(-1000.0) will be different
When it comes to runtime, JVM relaizes -1000 is not valid number, then IllegalAugumentException was thrown.
HTH
[ April 28, 2003: Message edited by: chi Lin ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic