aspose file tools*
The moose likes Java in General and the fly likes Programmatically or JVM thrown Exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Programmatically or JVM thrown Exception?" Watch "Programmatically or JVM thrown Exception?" New topic
Author

Programmatically or JVM thrown Exception?

Akbar Khan
Greenhorn

Joined: Jun 13, 2007
Posts: 24
Why NullPointerException is JVM thrown and NumberFormatException programmatically thrown?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not sure I follow what you mean. What do you mean by "JVM thrown"?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
JVM thrown Exceptions are also known as "unchecked Exceptions" while those which have to be programmatically handled are also called "checked Exceptions".
The first ones rise when something unexpected or that you don't know(want) to handle happens. The second ones are thrown when something wrong, but that you was in some way aware of, happens.

Now, NULL is a perfectly legal and meaningfull value. You can write perfectly correct code which deals with it. Often you even DESIRE to be able to receive null values. Java designers decided to let the single coder to choose how to deal with such problems. Moreover, but it's my own opinion...what would you think about reading at the code of a big proejct where you have to always use a try-catch block to simply handle methods parameters? it would be boring, wouldn't it? Better to live the single consider dangerous cases. Especially because he would usually know what to do.

What about a String containing a malformed/illegal number? What could an API class do receiving such a bad parameters? It cannot return NULL...it wouldn't be correct. Nor it could return a FALSE boolean value..'cause Java only allows a return value (maybe a Collection, but it always a reference to a single object). The method doesn't simply know what to do and it has no other way to communicate the application about the wrong situation...Therefore, an Exception. Checked 'cause we, programmers, already know it could happen and have no other way to prevent it.

This is my post here and English not my mother tongue...hope I have been enough clear.



PS: Thanks to Kathy and Bert for their wonderfull book. Thanks to it, it took me about 4 months in my free time (about 100 hours I think) to get the SCJP. But, most of all, it allowed me to greatly improve in my job...(got 3 carriers advancement in a couple months...and I just work since a year). Really thank you fellows, you rock


SCJP 5 & 6, SCWCD 5, SCBCD 5
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Runtime exceptions are something which are very fatal and there are absolutely no change for recovery (Like NPE).

Whereas Checked exceptions are exceptions that has to be caught as there might be scenarios from which you can continue with the flow of the program even after getting an exception.

For NumberFormatException you can set some default value to the variable in the catch block.


Rahul Bhattacharjee
LinkedIn - Blog
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


JVM thrown Exceptions are also known as "unchecked Exceptions"

You know, in all the years I've been writing Java/reading about Java I've never heard unchecked exceptions described as JVM exceptions. Thank you Bianchi Francesco for explaining that.

I'm still not sure I understand what Akbar Khan is asking though. Both NullPointerException and NumberFormatException are unchecked exceptions. Are you asking why the writers of Java deliberately wrote mehtods like Integer.parseInt(String str) to throw a RuntimeException rather than a checked exception?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'll have to think longer about "JVM exceptoins are unchecked" but I'm sure you can't say the converse: "unchecked exceptions are JVM exceptions" is not true. I make my own and throw and catch em all the time.

If null pointer was a checked exception you'd have to wrap almost every method call in a try-catch block. It's A Good Thing that what you've called "JVM exceptions" (still thinking on those) are unchecked!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Originally posted by Stan James:
I'll have to think longer about "JVM exceptoins are unchecked" but I'm sure you can't say the converse: "unchecked exceptions are JVM exceptions" is not true. I make my own and throw and catch em all the time.


Can't get the meaning of making your own unchecked Exception and then catching them.. Why not to directly go with checked ones if you think to put them in a try/catch block? Isn't this a little like a "dangerous obfuscating" solution? Or did I misunderstood whay you wanted to say?

Anyway, sorry guys for being unexact. No, definetely unchecked Exc. are by no means exclusively JVM thrown ones.

Anyway.. I think much more can be said about the difference of an Exc like NullPointer and NumberFormat but it would be a long speech.. (and mostly made up of speculations). Here some of my thoughts: I think much has to do with the fact that NullPointer are in a certain way bound to heap management (therefore with JVM native code), are somehow "structural critical exceptions" which cannot be easily foreseen by "java" code. Moreover it's maybe the most common and widespread Excep. in the Java platform. NumberFormatException, on the other side, is a very localized one, tipycally not a critical one and can be by far foreseen during the "number parsing" algorithm.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12836
    
    5
Please note that the distinction between checked and unchecked exceptions is only something that the Java compiler observes. There is NO DIFFERENCE at runtime in the way these are handled.

It is very important for beginning Java programmers to keep in mind the difference between compile time errors and runtime events.

Bill
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can't get the meaning of making your own unchecked Exception and then catching them.. Why not to directly go with checked ones if you think to put them in a try/catch block? Isn't this a little like a "dangerous obfuscating" solution? Or did I misunderstood what you wanted to say?


There is a school of thought that checked exceptions were not such a great idea to start with. After all, many other languages get along fine without them. Some people use unchecked exceptions when they can. I've tried it in a couple systems where it happened to work out quite nicely but I haven't tried to convert my overall style to unchecked exceptions.
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Originally posted by Stan James:


There is a school of thought that checked exceptions were not such a great idea to start with. After all, many other languages get along fine without them. Some people use unchecked exceptions when they can. I've tried it in a couple systems where it happened to work out quite nicely but I haven't tried to convert my overall style to unchecked exceptions.


Thanks Stan. Probably you are right but, at least in my opinion, such a choice goes along with a very well designed architecture. Probably the better context for such a solution to be really effective (and not only obfuscatin) is when you have a chain of methods which doesn't want to handle the Exception but to forward it to higher levels of the application. But, as i said, I think this is a solution only very skilled people should adopt. And if got right the Java spirit, they are making lots of efforts to make also programs written by newbies very tough and reliable. (just think about the introduction of generics in collections...was there any REAL need for apps written by expert people?).
And you are true..after all there are programs which get along very well without excpetion. But also without a compile-time type check... But I think there is a reason if you are programming in Java and not in php

Cheers,
Francesco.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programmatically or JVM thrown Exception?