Win a copy of Microservices Testing (Live Project) this week in the Spring 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

K&B Page 382 Exception

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, There are several exceptions(ClassCast, IllegalState, NullPointer, StackOverflow etc..) listed in page 382 K & B. How do we know that these exceptions will be thrown by JVM or programmatically.
 
Saloon Keeper
Posts: 14088
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By reading the contract of a class or method you use. It should tell which exceptions it throws and when.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan, I am under the impression that Checked Exceptions are thrown programmatically and Un-checked(Runtime) Exceptions thrown by JVM ? Could you please advice if this statement is correct or not ?
 
Stephan van Hulst
Saloon Keeper
Posts: 14088
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. While I'm not sure if the JVM itself throws exceptions (I imagine it throws stuff like OutOfMemoryError and InternalError), most exceptions are thrown programmatically (if you can even consider Error's thrown by the JVM to be non-programmatic). It doesn't really matter, you shouldn't care.

The fact of the matter is that regardless of where exactly exceptions have their origin, they 'bubble up' until they are either catched or thrown by the main method. This means that you should check the contract of any methods that you call, to see whether they are capable of throwing a certain exception. Methods that you write yourself can also throw exceptions, if you want them to. These can be both checked and runtime exceptions.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi Stephan, I am under the impression that Checked Exceptions are thrown programmatically and Un-checked(Runtime) Exceptions thrown by JVM ? Could you please advice if this statement is correct or not ?


I think, JVM throws only unchecked exceptions. But, we can programmatically throw checked and unchecked exceptions. For example, IllegalArgumentException, IllegalStateException & NumberFormatException are all unchecked exceptions, but they are thrown programmatically, not by JVM.
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you guys make a "JVM vs Programmatic" distinction between an IllegalArgumentException and an ArrayIndexOutOfBoundsException?
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its not about throwing the exceptions, its about catching the exceptions.

1. Compiler will not compile your file(.java) until you handle the checked exceptions as these are compulsory to handle....
2. However unchecked exceptions there are no restrictions over them you can manage them in whatever way you want...
3. And errors, we should not handle them let the JVM handle them...

Well it would surely be better if you code and try different combinations of them ..if you still have some problem then you can discuss....
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = Integer.parseInt("ABC"); throws IllegalArgumentException(if not caught by its subclass i.e., NumberFormatException).
String[] stringArray = new String[] {"Hi", "Hello"}; If we try to fetch any value from 2nd index(3rd element), we get ArrayIndexOutofBoundsException.
I personally feel that because of programmatic errors, JVM throws above 2 Runtime Exceptions.

Book says IllegalStateException (IS-A RuntimeException) thrown by Programmatically and ArrayIndexOutofBoundsException(IS-A RuntimeException) thrown by JVM.
It is hard to figure out the differences why one throws Programmatically while the other one throws by JVM.
Also, Why AssertionError (IS-A Error) thrown by Programmatically
Confusing...

Bert Bates wrote:Would you guys make a "JVM vs Programmatic" distinction between an IllegalArgumentException and an ArrayIndexOutOfBoundsException?

 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Programmatically throwing the exception means you are manually creating the object of an exception and throwing it. like this..

If you are not doing so, then the JVM does it for you for exceptions that generates at run time and this no more would be considered as Programmatically thrown exception....
 
Bert Bates
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...

Let me ask this a different way:

Imagine you were writing a utility class that other programmers might use later, can you envision:

1 - Somewhere in your code you decide to throw your own IllegalArgumentException?

2 - Somewhere in your code you decide to throw your own ArrayIndexOutOfBoundsException?

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert Sir, I am not sure. Could you please explain how to differentiate..I am not able to envision.
 
Bert Bates
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say I want to write a method for other programmers to use. Let's say that the idea of this method is to convert hexadecimal numbers into binary. If someone is using my method and they pass my method an argument that isn't formatted as a hexadecimal string, I might want to throw an IllegalArgumentException. That's me, a programmer, throwing an exception programmatically.

Now, I can't think of a similar situation where I'd want to throw an ArrayIndexOutOfBoundsException. Typically, that exception is thrown by the JVM to indicate some sort of bug in some code that's messing with arrays.

hth,

Bert
 
reply
    Bookmark Topic Watch Topic
  • New Topic