Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B question - overriding methods

 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone

Here is a question from chpater 5 of SCJP5 book of K&B.


Answ is A , D, E, and F are correct. I understand for A & D. But in E & F the NumberFormatException is subclass of IllegalArgumentException and these two are Programmatic exceptions. How come these be considered a valid override. Please somebody help me understand.

 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NumberFormatException is an unchecked exception. An overriding method can add as many unchecked exceptions to the throws clause as it wants. This has nothing to do with Programmatic or non-programmatic exceptions...
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh thanks so much. I was thinking that programmatic are checked ones or so.

So why this demarcation Programmatic and JVM generated exception. How does it help? In terms of handling exceptions and writing code for that.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The categorization of Programmatic and JVM exceptions is logical. JVM exceptions are thrown automatically by the JVM (they are unchecked) and usually you are not supposed to throw them manually. On the other hand Programmatic exceptions are always thrown using the throw clause (they can be checked or unchecked). The K&B book contains a list of exceptions in the exception hierarchy that you should know for the exam...
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not still clear about that. When you say that programmatic ones have to manually thrown - do you say that in try{} block if such an exception occurs it will not go to catch block.

Let me put is this way - say we have this code Now how will it behave when
1. the code in the try block causes some JVM thrown exceptions
2. the code causes programatically thrown exception.

thanks for all your effort buddy.

 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can catch both Programmatic and JVM exceptions using a catch block. You are not getting the difference between the two. Lets take an example of this code



Now this code will throw an AirthmeticException. This exception will be thrown by JVM. There is no throw clause involved in throwing of this exception. On the other hand, when you execute this code



This code will throw NumberFormatException. Now if you see the code of parseInt method, then it would look something like this



So basically Programmatic exceptions are always thrown using the throw clause. There is no way that they can be thrown by the JVM without any throw clause...
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit. now I got the point.. it is the API code which is explicitly throwing these exceptions. This was really cool explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic