• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception: Programmtically or by JVM?

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I often encounter this type of question in mock exams..

"Which of these exception are thrown by JVM instead or Programmer?"

As per i think,Every exception thrown by JVM is because of programmer mistake..!!for Example : NullPointerException...This happens becuase we are trying to invoke some methods on an object which is null.




Now this code will throw a null pointer Exception..!! Now this is because programmers mistake..!! So it should not come under JVM category..!!

I think i dont understand the meaning of "exception thrown by JVM instead or Programmer?"
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
all exceptions are thrown by jvm only
but they are some
exceptions that should be handled by programer as they terminate the program abruptly.
 
Greenhorn
Posts: 15
Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes according to K&B (page no. 367) NullPointerException is a jvm Thrown exception.
Programmatically Thrown Exceptions are those which are created by an application and/or API developer . like IlligalArgumentException IlligalStateException NumberFormatException AssertionError
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with your points..but how do you recognize which exception comes under which category..?

Why should i refer to book to check whether NullPointerException is thrown by JVM or Me??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no real, clear difference between exceptions thrown by the JVM or by the programmer (or the Java program running in the JVM). Some exceptions, like NullPointerException, are thrown by the JVM under certain circumstances, but you can also throw NullPointerException yourself:

throw new NullPointerException("Something is null");

I'm not 100% sure, but I think that this is not something you really need to know for SCJP.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think all exceptions which need to be caught by us come under programmatic..rt?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My thoughts

I interpreted those exceptions thrown by the JVM to be exceptions/errors I wouldn't throw myself in my code. I've never thrown a NullPointerException, ArrayIndexOutOfBoundsException nor an ExceptionInInitializerError.

I have thrown IllegalArgumentException when my method has got arguments of the correct type, but invalid content.

Cheers,

Paul
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all!

i totally agree with Paul Brabban. A programamatic exception, is one that i actually throw in my code. Of course that you can throw any exception, but there are exceptions that you usually do not throw, like : throw ArrayIndexOutOfBoundsException() or NullPointerException.
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but is there some rule that decide the difference b/w the two!
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because you can throw any Exception or Error with a "throw new ..." statement in your Java program.

There's no exact rule to distinguish "programatically thrown" and "thrown by the JVM" exceptions. The original question:

"Which of these exception are thrown by JVM instead or Programmer?"

is meaningless and has no precise answer. You could ask

"Which of these exception are usually thrown by JVM instead or Programmer?"

but then the question would still not have a really clear answer.
[ February 26, 2008: Message edited by: Jesper Young ]
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me it was a little confusing until I realized that the APIs aren't the JVM.

For example, you'll see NumberFormatException being thrown by the wrapper classes and it looks like the JVM but it's not - it's being thrown by an API, which falls in the Thrown Programmatically category for me.

Thanks for the question though - made me sit down and clear my thoughts on the subject.

I don't know about rules - if it's not intuitive to you, you're probably just better off learning them off by heart, I expect that'll get you through the exam question.

Cheers

Paul
reply
    Bookmark Topic Watch Topic
  • New Topic