This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B question - overriding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B question - overriding methods" Watch "K&B question - overriding methods" New topic
Author

K&B question - overriding methods

Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
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.


Programmer Analyst || J2EE web development/design
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Mar 26, 2009
Posts: 131
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Mar 26, 2009
Posts: 131
Thanks Ankit. now I got the point.. it is the API code which is explicitly throwing these exceptions. This was really cool explanation.
 
Consider Paul's rocket mass heater.
 
subject: K&B question - overriding methods