This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

AssertionError, most commonly thrown by whom?

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is AssertionError most commonly thrown by the JVM, or programatically?

Thanks,

Ruben
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Is AssertionError most commonly thrown by the JVM, or programatically?

Thanks,

Ruben



Programmtically. To be thrown programmtically would be:
assert i < 2;
assert false;

The difference in semantics to being thrown by the JVM vs programatically isnt too clear, but from the study guidelines it says it for AssertionError its programatically. From this you can infer that by programatically, they mean that if you're writing specific code such as above to throw an Exception or Error rather than it occurring from exceptional/erroneous events, then it is more commonly thrown programatically.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert O'Leary wrote:

Ruben Soto wrote:Is AssertionError most commonly thrown by the JVM, or programatically?

Thanks,

Ruben



Programmtically. To be thrown programmtically would be:
assert i < 2;
assert false;

The difference in semantics to being thrown by the JVM vs programatically isnt too clear, but from the study guidelines it says it for AssertionError its programatically. From this you can infer that by programatically, they mean that if you're writing specific code such as above to throw an Exception or Error rather than it occurring from exceptional/erroneous events, then it is more commonly thrown programatically.


Thanks for your answer Robert. But I think that when an ExceptionError is thrown because of a failed assert statement (which is the way AssertionError is most usually thrown,) it is the JVM which actually throws the AssertionError.

I'm going to elaborate a little bit. When you say that an exception/error/throwable is thrown programmatically, you are saying that there is actual Java code somewhere in your program where you have a throw statement throwing it. For example, if you are coding a method and you can determine that one of the arguments is not in the required range for the method to work properly, you could throw an IllegalArgumentException.

In the contrary, a NullPointerException is thrown by the JVM as a result of using a null reference to attempt to access a non-static member. You wouldn't throw a NullPointerException explicitly in your code, the same that normally you wouldn't throw an AssertionError (but an AssertionError is thrown by the JVM at runtime as a result of a failed assert statement.)

These are the reasons why I think AssertionError is typically thrown by the JVM (and not programatically.) Now, I fear that if I see this question in the exam I will answer it right but lose a point because of it. So I'm hoping someone can clear this point.
 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks for your answer Robert. But I think that when an ExceptionError is thrown because of a failed assert statement (which is the way AssertionError is most usually thrown,) it is the JVM which actually throws the AssertionError.



No according to the Kathy Sierra and Bert Bates book, youre incorrect. Look at page 382 (Chapter 5 -table of Exceptions). It says that AssertionError is "typically thrown" programatically

IllegalArgumentException, IllegalNumberFormatException (subclass of IllegalArgumentException), and IllegalStateException are typically thrown programatically.

Thats what they say. Draw your own conclusions.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert O'Leary wrote:


Thanks for your answer Robert. But I think that when an ExceptionError is thrown because of a failed assert statement (which is the way AssertionError is most usually thrown,) it is the JVM which actually throws the AssertionError.



No according to the Kathy Sierra and Bert Bates book, youre incorrect. Look at page 382 (Chapter 5 -table of Exceptions). It says that AssertionError is "typically thrown" programatically

IllegalArgumentException, IllegalNumberFormatException (subclass of IllegalArgumentException), and IllegalStateException are typically thrown programatically.

Thats what they say. Draw your own conclusions.


I wasn't arguing that that's not what is said in the book. I know that the book says that AssertionError is thrown typically programatically. I just don't understand how that's the case. I really need to rationalize things, and this has always stuck as one of those things that should be questioned and discussed. For the reasons I give above, I think an AssertionError is similar to a NullPointerException (thrown typically by the JVM.) You can make your code throw an AssertionError at runtime the same that you an make it throw a NullPointerException at runtime (without using a throw statement.) But to me that causes the JVM to throw the error, it is not thrown programatically as long as you don't use an explicit throw statement in your code. Maybe Bert can chip on this one.
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic