• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Distinguishing b/w programmer thrown exceptions and JVM thrown exceptions.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,

I am preparing for SCJP 6 . I have a doubt as to how can we distinguish b/w JVM thrown exceptions and exceptions thrown by programmers. Is there any rule to do so? Thanks in advance.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it depends on who you classify as "programmers". The JVM was built by programmers, so every exception it throws could be considered a programmer thrown exception. Then again, your own code is run by the JVM, so every exception you throw could be said to be a JVM thrown exception.

It's a bit of a strange distinction to make.
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you perhaps referring to checked vs unchecked exceptions?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the exam does occasionally make a distinction between exceptions that are (generally) thrown by the JVM and those that are explicitly thrown in application code.

I've always found the key is simply remembering what the exceptions actually mean - under what circumstances they are thrown. If you properly understand this then it's usually pretty obvious. For example: NullPointerException. That's thrown by the JVM whenever you try to dereference a reference with a null value. Whereas an IllegalArgumentException is thrown when a value passed in to a method isn't valid. But the JVM is never going to know that - that depends on application logic. It's the result of code like this:
So I never bothered learning which category exceptions came into. I learned what they were for, and worked it out from that. If you can look at a piece of code that doesn't have a new XyzException() in it and say "that might throw an exception", then it must be a JVM thrown exception.
 
author
Posts: 84
5
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
The Exception class has a getStackTrace method that returns a StackTraceElement array. The StackTraceElement class has a number of methods that return the file name and method name of the source of the exception. Conceivably this could be used to determine whether the exception was thrown by a JDK class or a "programmer" class. I am not sure if this is what you are looking for.
 
Parsuram Samal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew , I understood what you have said but still i hava a small doubt, i.e. we can throw both JVM thrown exceptions and Exceptions thrown by programmers in our program by using the throw keyword, then how can we exactly differntiate between these two. Do we have to focus on what exactly the exception means and its purpose as you have said and from that distinguishing these exceptions as JVM thrown exceptions or Exception thrown by Programmers or do we have to do anything else. I think if this doubt is cleared the question is solved. Thanks .
 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's like Stephan said: it's a meaningless distinction. There is no meaningful answer to your question. And since you posted it in the forum about certification exams, I'm pretty certain you won't ever find that question on one of those exams.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I think Parsuram is correct, here's the objective description for the OCP 6 exam:

Recognize situations that will result in any of the following being thrown: ArrayIndexOutOfBoundsException,ClassCastException, IllegalArgumentException, IllegalStateException, NullPointerException, NumberFormatException, AssertionError, ExceptionInInitializerError, StackOverflowError or NoClassDefFoundError. Understand which of these are thrown by the virtual machine and recognize situations in which others should be thrown programatically.

 
Master Rancher
Posts: 5120
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I don't think it's really meaningless, either. It's useful to know, when you are studying a stack trace for a NullPointerException or ClassCastException, that you are not looking for a line that says "throw new NullPointerException". Usually. When it's an exception thrown by the JVM, it's important to understand what it looks like in your code, and to mentally fill in what the JVM does in response.
 
Mike Simmons
Master Rancher
Posts: 5120
82
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Parsuram Samal wrote:Thanks Matthew , I understood what you have said but still i hava a small doubt, i.e. we can throw both JVM thrown exceptions and Exceptions thrown by programmers in our program by using the throw keyword, then how can we exactly differntiate between these two.


Any exception (or Throwable really) can always be thrown intentionally by a programmer who chooses to do so using the throw keyword. (provided they obey the usual rules, declaring or catching checked exceptions, etc.) The key is to understand the other situations where an exception gets thrown by the JVM, without any "throw" appearing in source code.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Digging this thread up as I was reviewing this objective... I too was confused, not finding any distinction in the API for the listed exceptions and errors, then remembered seeing a table in the Sierra/Bates SCJP 6 Study Guide.

According to that we have:

Thrown by JVM:
NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, StackOverflowException, ExceptionInInitializerError, NoClassDefFoundError

Thrown Programmatically:
NumberFormatException, AssertionError, IllegalArgumentException, IllegalStateException

Like others have said, when you understand when/why each is thrown, it becomes fairly obvious whether they are thrown by the JVM or programmatically, but having a list helped dispel any doubts I had, and made it more obvious to me why they were categorized that way.
 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Waking up an old thread since I am not clear.

Is it true that the exceptions which are thrown by in built classes in jdk are jvm exceptions and those thrown by me as a programmer in my application are programmatic exception? Also isnt the job of the jvm to handle exception as a default handler in case I do not handle the exception in my code? Is the job of the jvm to throw exception or handle exception?
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic