aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Identifying Exception types Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Identifying Exception types" Watch "Identifying Exception types" New topic
Author

Identifying Exception types

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
I am getting confused in identifying whether an exception is thrown by JVM or by the Program.
Why are ArrayIndexOutOfBounds and NullPointerException thrown by the JVM, whereas IllegalArgumentException thrown by the application?
Can someone please explain?
Mohit Mehta
Greenhorn

Joined: Jan 24, 2005
Posts: 25
ArrayIndexOutOfBounds and NullPointerException thrown by the JVM

whereas IllegalArgumentException thrown by the application


I don't think that ArrayIndexOutOfBounds and NullPointerException thrown ONLY by the JVM, you can check for Null value and throw same exception likewise IllegalArgumentException can be thrown by JVM also...

all these 3 exception falls into RuntimeException categroy so usally we should expect it to be thrown by JVM.


Regards,<br />BulletProof Monk.<br /> <br />"Nthing the Mind of man can Conceive<br />and Believe,It can achieve"
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Originally posted by Mohit Mehta:

I don't think that ArrayIndexOutOfBounds and NullPointerException thrown ONLY by the JVM, you can check for Null value and throw same exception likewise IllegalArgumentException can be thrown by JVM also...

all these 3 exception falls into RuntimeException categroy so usally we should expect it to be thrown by JVM.


IllegalArgumentException will never be thrown by JVM. It is always thrown programmatically using

throw new IllegalArgumentException

if you see any method in the API which throws IllegalArgumentException, then you are sure that somewhere in the beginning of the method, it has code like this



Also you are not supposed to throw ArrayIndexOutOfBoundsException or NullPointerException on your own. It is for JVM. Just think about it, when will you need to throw these exceptions. Suppose your method says that no one MUST pass a null argument to it, then you will not write your method like this



You are supposed to code your method like this-



It's not about you CAN or not. It's about what you MUST do. You can create your method and throw NullPointerException, but it is against the standards of coding...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Thanks, Ankit.
I'm still not very clear. There are exam questions which ask you to identify if an exception is thrown by the JVM or progamatically thrown.
How do you identify?
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

See and understand checked and unchecked exceptions.

I think that's what you are looking for.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

There is a list of JVM and programming exceptions in the K&B book. refer to that (I don't remember the page number)...
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Identifying Exception types
 
Similar Threads
finalize() method of garbage collection...HELP needed....
Exception being thrown by JVM / Programmatically
Error and Exception
AssertionError clarification