Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Identifying Exception types

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See and understand checked and unchecked exceptions.

I think that's what you are looking for.
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate 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 a list of JVM and programming exceptions in the K&B book. refer to that (I don't remember the page number)...
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic