File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exam lab q-9(excepton handling) 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 "exam lab q-9(excepton handling)" Watch "exam lab q-9(excepton handling)" New topic
Author

exam lab q-9(excepton handling)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


above code works fine.
but at line 1 ,run-time exception has never been thrown,so i guess it should be the error coz "i have read not to use the exception that has never been thrown in corresponding try block else compiler error".
please justify above statement.why it is not error?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Arjun!

The RuntimeExceptions are not checked if they are thrown from the try block, because they are... unchecked.

They can occur or don't so there is no way you can say they will ever be thrown from the try clause.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Pedro Kowalski wrote:Howdy
The RuntimeExceptions are not checked if they are thrown from the try block, because they are... unchecked.
They can occur or don't so there is no way you can say they will ever be thrown from the try clause.

howdy
that means RuntimeExceptions if used unnecessarily in catch will not harm.
but checked exceptions are evil in this case.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Just try it for yourself:



Will it compile?
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Pedro Kowalski wrote:Just try it for yourself:

Will it compile?


offcourse yes,works fine.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Hope that helped mate :-)

Cheers!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hey Arjun! If the compiler checks whether the exception will be thrown or not, then that exception IS NOT a RuntimeException at all!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

yes,thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: exam lab q-9(excepton handling)
 
Similar Threads
unreachable catch block
Polymorphism w.r.t. overloading
Exception handling: Throw not initialized - can someone explain this please?
Casting Throwbale to Exception
need help on Exceptions