It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Distinguishing b/w programmer thrown exceptions and JVM thrown exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Distinguishing b/w programmer thrown exceptions and JVM thrown exceptions." Watch "Distinguishing b/w programmer thrown exceptions and JVM thrown exceptions." New topic
Author

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

Parsuram Samal
Greenhorn

Joined: Feb 12, 2010
Posts: 8

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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Are you perhaps referring to checked vs unchecked exceptions?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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.
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

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.


Richard Reese
Java 8 New Features: A Practical Heads-Up Guide
Parsuram Samal
Greenhorn

Joined: Feb 12, 2010
Posts: 8
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 .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
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.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
John Wilson
Greenhorn

Joined: Dec 04, 2013
Posts: 3
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Distinguishing b/w programmer thrown exceptions and JVM thrown exceptions.