Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AssertionError clarification

 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I suppose programmatically thrown exceptions mean those exceptions which are thrown
usign throw keyword ...
In K&B it is written that AssertionError is programmatically thrown ,
Is it exception to JVM thrown exceptions??
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is difference between Error and Exception.

Yes Assertions are thrown programmatically but the their purpose is different.

There will be difference given in K&B book only.

Its very common and if I start explaining the concept here then it will make a huge post.

You can also refer wikipedia for Java Assertions.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sunny...
I just wanted to know how AssertionError is considered
to be programmatically thrown or that definition is too general,
apart from that i think i understood asertions....
 
Dan Daemler
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that statement a little hazy myself. After re-reading pg. 379-380:

JVM exceptions
Those exceptions or errors that are either exclusively or most logically thrown by the JVM.

Programmatic exceptions
Those exceptions that are thrown explicitly by application and/or API programmers

JVM: If you try to call a method on a null reference variable, the JVM will throw a NullPointerException.

Programmatic: By putting an assert statement in your code you are actively deciding to throw an AssertionError if your assumption isn't true.

I guess that's why they consider it programmatically thrown. An AssertionError will never be thrown unless you go out of your way to use assertions in your code and/or turn on assertions when you run the program. Just like some of the other exceptions won't be thrown unless you (or one of the API programmers) decide to throw them. On the other hand, a NullPointerException can be thrown by the JVM whether you mean to or not.

Since I still don't know 100% which exceptions are programmatic vs. JVM, I will just memorize the chart on page 382.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Dan Daemler ,
I too assumed it in the same way...
Also note that all JVM thrown exceptions are unchecked and
programmatically thrown exceptions are may be checked or unchecked...

A good tutorial about exceptions...
http://www.jforeach.com/understanding-checked-and-unchecked-exceptions-in-java/59
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic