• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

NumberFormatException thrown programmatically

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

Maybe someone can enlighten me:)

In OCA Study guid for OCA 8 from Boyarski and Selikoff. In Chapter 6 ( and in Javadoc from Oracle) NumberFormatExceptions are a child class of RunTimeException. this far I understand the Point with checked and unchecked.

But than in the Review Questions of that chapter , Question 5 I am asked  which of the following exceptions are thrown by the JVM and
obviously i choosed NumberFormatException as  an answer.

But it isn't thrown by JVM as the solution told me but a programmatically thrown one through Wrapper class.

And this is something I don't get right.

Because nearly everywhere I was told Runtime Exception are thrown by JVM ..
I marked this as something  like a rule to remember , but now ;( …

Is there someone to explain that to me ? Or is it just a Special kind of rule I have to remember , and if yes,are there some more specialties coming with that topic ?

Thanks for helping me out
Alex
 
Master Rancher
Posts: 3313
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All "exceptions" (Throwables) thrown by the JVM are either RuntimeExceptions, or Errors.  However, not all RuntimeExceptions or Errors are thrown by the JVM.  There are many, many cases where these can be thrown programmatically as well.  Even specific exceptions that are thrown by the JVM, like NullPointerException or ClassCastException, may be thrown programmatically as well, if someone feels like writing code that does that.

So, you can learn specific situations where the JVM will throw a RuntimeException, or an Error.  But never assume that's the only way that RuntimeException or Error can be thrown.
 
Ranch Hand
Posts: 274
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aleana Cologne wrote:Hi everybody,

Maybe someone can enlighten me:)

In OCA Study guid for OCA 8 from Boyarski and Selikoff. In Chapter 6 ( and in Javadoc from Oracle) NumberFormatExceptions are a child class of RunTimeException. this far I understand the Point with checked and unchecked.

But than in the Review Questions of that chapter , Question 5 I am asked  which of the following exceptions are thrown by the JVM and
obviously i choosed NumberFormatException as  an answer.

But it isn't thrown by JVM as the solution told me but a programmatically thrown one through Wrapper class.

And this is something I don't get right.

Because nearly everywhere I was told Runtime Exception are thrown by JVM ..
I marked this as something  like a rule to remember , but now ;( …

Is there someone to explain that to me ? Or is it just a Special kind of rule I have to remember , and if yes,are there some more specialties coming with that topic ?

Thanks for helping me out
Alex



Thanks Aleana for this question.  Welcome to the Ranch!  This wasn't on my radar until you pointed it out!  

Mike Simmons wrote:There are many, many cases where these can be thrown programmatically as well.  Even specific exceptions that are thrown by the JVM, like NullPointerException or ClassCastException, may be thrown programmatically as well, if someone feels like writing code that does that.

So, you can learn specific situations where the JVM will throw a RuntimeException, or an Error.  But never assume that's the only way that RuntimeException or Error can be thrown.



To Mike's point, From the book you referenced in the Runtime Exceptions section:

They can be thrown by the programmer or by the JVM.

 Thus, the "only" rule is that

They don't have to be handled or declared.

 
Aleana Cologne
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi @ all
Thank you so much for helping me out with that Topic and I got the Point now
 
Charles O'Leary
Ranch Hand
Posts: 274
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aleana Cologne wrote:Hi @ all
Thank you so much for helping me out with that Topic and I got the Point now


Thanks again Aleana for highlighting this canned gotcha!  Indeed, according to Oracle

RuntimeException is the superclass of those exceptions that  can be thrown during the normal operation of the Java Virtual Machine.

 
Marshal
Posts: 65383
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you supposed to know that the wrapper class throws such exceptions? Does it say anything in the book or the documentation that the method throws its own exceptions?
Gp and look through your Java® installation folder; there is a file called src.zip in there (at least there often is). Unzip that file and go to the .../java.base/java/lang/Integer.java file, and in the code for the parseInt(String, int) method, you will find all sorts of lines like this:-That confirms what CO'L told you about how such exceptions are thrown, but how would you know without looking at the source?
 
Aleana Cologne
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How are you supposed to know that the wrapper class throws such exceptions? Does it say anything in the book or the documentation that the method throws its own exceptions?
Gp and look through your Java® installation folder; there is a file called src.zip in there (at least there often is). Unzip that file and go to the .../java.base/java/lang/Integer.java file, and in the code for the parseInt(String, int) method, you will find all sorts of lines like this:-That confirms what CO'L told you about how such exceptions are thrown, but how would you know without looking at the source?



Thats exactly what I did before I thanked earlier for helping me ;)

But anyway Thanks again for telling me the source to find it:)
 
pie. tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!