my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question!! please sovlve!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question!! please sovlve!!" Watch "Exception question!! please sovlve!!" New topic
Author

Exception question!! please sovlve!!

himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
The following code is attempting to write a user-defined exception class. Which option can be inserted at Line 01 to ensure successful compilation?

class Exp extends java.io.IOException {
//////////// Line 01 //////////////
v_method( );
}

void v_method( ) throws Exp {
throw new Exp( );
}
}

void k_method( ) {
void k_method( ) throw Exp {
void k_method( ) throws Exp {
void k_method( ) assert Exp {
void k_method( ) try Exp {


kindly,tell me which one is the answer i got this from irix exam!!


SCJP 5
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
i am not sure , i guess the answer is 1 & 3.

My Techie Blog - Java Questions
Kashif Sayyed
Greenhorn

Joined: Dec 18, 2007
Posts: 9
The correct answer is,

void k_method( ) throws Exp { => k_method shud either handle or declare exception thrown in v_method


void k_method( ) { => it will not compile as it doesnt declare or handle exception thrown in v_method

void k_method( ) throw Exp { => throw keyword can not used to declare exception,it shud be throws

void k_method( ) assert Exp { => wrong method declaration

void k_method( ) try Exp { => wrong method declaration

A signature always reveals a man's character - and sometimes even his name.
Rizvan Asgarov
Ranch Hand

Joined: Jan 07, 2009
Posts: 451

Hi himanshu,

The answer is: void k_method( ) throws Exp {

void k_method( ) { - This is unhandled exception

void k_method( ) throw Exp { - Throw keyword is used to pass a custom message to the exception handling module i.e. the message which we want to be printed.

void k_method( ) assert Exp { - assertions should be used to verify the assumptions so that we can guarantee that the application always executes complying with the specifications. assertion is basically a mechanism which helps us writing correct programs. Whereas Exception is a mechanism of checking if the implementation is executing without any expected or unexpected errors or not.

void k_method( ) try Exp { - I think this must be used between try and catch block

Do additions...


"Half of the science is to ask question"
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
You can throw it to the ceiling or catch it and put it in its place....
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
ok, that i figured it out even at that time that throws Exp otion was correct.........but the thing is that if this method is also throwing the exception then any class that will extend class Exp must deal with this Exception......!!
so it can be said just writing this code will..........compile the program fine!!
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31
Hi I am just wondering how can you determine it is a checked exception, it is pretty confusing since to me all the exception should be run time exception, so how can I tell if it is a subclass of runTimeException or not?(Since I know runTimeException and its subclass is not checked). Thanks!
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31
Sorry my actual question is "How can I know if an exception is a subclass of RuntimeException"
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"sdfsdf asfsdf " welcome to Javaranch
please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31
changed
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Hello myKitty wrote:Sorry my actual question is "How can I know if an exception is a subclass of RuntimeException"


Well there is no automatic way of doing this. You can either create a sample program to test it or read the documentation of the class to find out if it is a sub-class of RuntimeException. The SCJP exam covers a small number of exceptions which are given in a table in the K&B book. Normally you will be concerned with NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException.

Also I think your name is still not according the the javaranch naming policy...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31
Thank you Ankit, can I interpret it as if it is thrown by the JVM, it is unchecked, if it is programatically it is checked from that exception list from the book?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

every exception which is in the objective of scjp 1.5 are runtime exception except the IOException(FileNotFoundException & EOFException) and runtime exception are not checked i.e there is no need to handle or declare it.

i too had a lot of confusions in it.
check this out.. this will help alot.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/package-tree.html

moderators, this is my first post.if i am violating any rule while posting,please notify.


SCJP 5.0 SCWCD 5.0
Paolo Dina
Ranch Hand

Joined: Aug 15, 2008
Posts: 63
avi sinha wrote:every exception which is in the objective of scjp 1.5 are runtime exception except the IOException(FileNotFoundException & EOFException


This is true but there are other checked exceptions you have to care of for the exam that objectives don't cite. For example java.lang.Object's wait/notify/notifyAll throw an InterruptedException that you have to handle with a try/catch or declare in the method declaration.


SCJP 5
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31
Thank you guys, just for curiosity, why Java is designed that way to mandatorily make user handle checked exceptions ? What is the benefit of it? Why it is bad to allow user deciding if they want to do clean-up then add the catch clause themselves otherwise treat the exception as a run-time exception, sorry maybe this is nonsense but it will help me to know the real difference between the checked exception and the run-time exception. THANKS>
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
Hi all,
Thank you guys, just for curiosity, why Java is designed that way to mandatorily make user handle checked exceptions ? What is the benefit of it? Why it is bad to allow user deciding if they want to do clean-up then add the catch clause themselves otherwise treat the exception as a run-time exception, sorry maybe this is nonsense but it will help me to know the real difference between the checked exception and the run-time exception. THANKS>


Excception as the name suggests is omething which is rather unusual something rare caused due to some incorrectness of the program and usually thrown by JVM but sometimes the programmer can also throw exceptions. Now when an exception is raised and let's suppose the programmer doesn't take care of it..it will result in the sudden death of the running program. Checked exceptions are exceptions that needs to be handled using a try catch block or throws clause.
If you don't take care it will be a compilation error.
However RuntimeExceptions are exceptions which are unchecked in nature...that means yu don't have to take care of them you can even throw it anywhere without try catch or throws clause. It will result in the death of your program if you don't catch it.
In order to create a checked exception create any class and write extends Exception for runtime exceptions change Exception to RuntimeException.
Benefit of exceptions: Suppose you are using a banking system to monitor over accounts. You can easily handle cases of overdrawn money from accounts by using exceptions. There are several such examples. I hope it answers your question.


regards,
Arka
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31
Arka Guhathakurta wrote:Hi all,
Thank you guys, just for curiosity, why Java is designed that way to mandatorily make user handle checked exceptions ? What is the benefit of it? Why it is bad to allow user deciding if they want to do clean-up then add the catch clause themselves otherwise treat the exception as a run-time exception, sorry maybe this is nonsense but it will help me to know the real difference between the checked exception and the run-time exception. THANKS>


Excception as the name suggests is omething which is rather unusual something rare caused due to some incorrectness of the program and usually thrown by JVM but sometimes the programmer can also throw exceptions. Now when an exception is raised and let's suppose the programmer doesn't take care of it..it will result in the sudden death of the running program. Checked exceptions are exceptions that needs to be handled using a try catch block or throws clause.
If you don't take care it will be a compilation error.
However RuntimeExceptions are exceptions which are unchecked in nature...that means yu don't have to take care of them you can even throw it anywhere without try catch or throws clause. It will result in the death of your program if you don't catch it.
In order to create a checked exception create any class and write extends Exception for runtime exceptions change Exception to RuntimeException.
Benefit of exceptions: Suppose you are using a banking system to monitor over accounts. You can easily handle cases of overdrawn money from accounts by using exceptions. There are several such examples. I hope it answers your question.


THANK YOU!
 
 
subject: Exception question!! please sovlve!!