• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception question!! please sovlve!!

 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not sure , i guess the answer is 1 & 3.
 
Kashif Sayyed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rizvan Asgarov
Ranch Hand
Posts: 462
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can throw it to the ceiling or catch it and put it in its place....
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my actual question is "How can I know if an exception is a subclass of RuntimeException"
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changed
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic