my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes throw an Exception.... 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 "throw an Exception...." Watch "throw an Exception...." New topic
Author

throw an Exception....

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
SOURCE:www.javabeat.net


(1) Compilation error
(2) Runtime error
(3) Compile successfully, nothing is printed
(4) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo

My answer is (4).
Because exception which is thrown in throwMethod() will be caught by catch and it displays
�Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo�.
But the answer is (1).
Can anyone clarify me?



SCJP5 and SCWCD1.5
Think Twice Act Wise...
Aditya Keyal
Ranch Hand

Joined: Dec 01, 2008
Posts: 71
The method throwMethod() explicitly throws an exception. But it is not handled within that method nor it specifies that it throws an exception.

Thus the compilation error.

If instead of a explicit exception it was a runtime exception ex. divide by zero or array out of bounds then your answer would be correct.


alt: add a throws IllegalAccessException to the signature of the throwMethod and yiour answer will be displayed.


- Aditya Webservices Blog
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
if i modified to this



output is: Inside throwMethod.
finally :

why it is not showing "Caught."?


Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
Hi

By rule if you throw a exception in your method, the method must handle or declare the exception.

Just declaring that the method may throw IllegalAccessException (as you did) does not ensure that exception will be thrown in that method.

You have commented the line "throw new IllegalAccessException("demo");" in your throwMethod(). The code has executed normally and the catch block has not executed.


Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

See the throws keyword tells to the complier that "I know the throwMethod() is risky and it throws IllegalStateException". By doing this you don't have to handle the exception as it occurs. When the exception is thrown the control goes to the calling method...

When you do throw new IllegalAccessException("demo") a new exception is thrown and the control goes to the caller method where it is handled and the message is printed inside catch...

But when you comment the line no new exception is thrown and the execution is completed without any exception...


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Ganeshkumar,

throws never means it will throw all the time. Its just safety that
compiler demands in case of checked exception when you are not handing
the exception but declaring it.

Exception may or may not be thrown, irrespective of whether you have declared
that in method signature.


cmbhatt
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi chandra...

do you mean sometimes it can throw an exception?

if i remove try,catch,finally in main method it is showing a compilation error.

but if i mention it is not showing statement in output which means exception is not handled...
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Originally posted by Ganeshkumar cheekati:

if i remove try,catch,finally in main method it is showing a compilation error.



If you remove try,catch from the main method and an exception is thrown...the exception might be saying exception is not handled by your code.
As i said when an exception occurs either you handle it or declare it...

If the exception occurs and you declare it but not handle it the execution is sent back to the caller method...which in turn is supposed to handle the exception....In your case the main() is the caller...So execution is ducked to main() from the risky method...And in main() the compiler finds that neither have you handled the exception nor have you declared it.
Hence the compilation fails.
[ December 04, 2008: Message edited by: Sudipto Shekhar ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Ganeshkumar,

By declaring an Exception, its not necessary that you must throw the
exception. But compiler wont escape if you are throwing a checked exception
without declaring or handing it with try catch.


I think its clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throw an Exception....
 
Similar Threads
JavaCap no. 10
Java Caps Mock
Exception catching
Exception Q?
Exciting important exception question -- please help