About Question enthuware.ocajp.i.v7.2.1097 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
CreepyMulder
Posts: 4
Joined: Sat Jul 27, 2013 3:45 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1097 :

Post by CreepyMulder »

Hello o/,

Why isn't there any compilation issue as Exception is never thrown in the Try block? Is it because somewhere in the System.out.println method there can be an Exception thrown?

If I modify the code like that :

Code: Select all

	      try{
	          amethod();
	          System.out.println("try");
	       }
	       catch(FileNotFoundException e){
	          System.out.println("catch");
	       }
	       finally   {
	          System.out.println("finally");
	       }
	       System.out.println("out");
It does not compile as "FileNotFoundException" is never throw. How do the compiler can figure that FileNotFoundException is never thrown, but doesn't complain about "Exception" ?

Thank you ;)

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1097 :

Post by admin »

Very good question.

The issue is that RuntimeException is a subclass of Exception and any exception that extends RuntimeException need not be declared in the throws clause. Because of this reason, when you have catch(Exception e), the compiler can never tell whether the code in try catch will not throw any exception (because it is always a possibility that a RuntimeException, which is-a Exception may be thrown). So it has to allow the catch(Exception e) part.

However, with any other checked exception such as FileNotFoundException, compiler can easily determine if this exception is never thrown. Because if it is thrown, it would be present in the throws clause of the method. Thus, if it sees that there is no method call in the try block that has "throws FileNotFoundException", it can generate an error saying this exception is never thrown.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

CreepyMulder
Posts: 4
Joined: Sat Jul 27, 2013 3:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1097 :

Post by CreepyMulder »

Thanks ! It make more sense now ;)


mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1097 :

Post by mjmsausava »

In case of the modified code above (copied below):

Code: Select all

 try{
             amethod();
             System.out.println("try");
          }
          catch(FileNotFoundException e){
             System.out.println("catch");
          }
          finally   {
             System.out.println("finally");
          }
          System.out.println("out");
the amethod() will declare the exception in its signature because its specifically modified to a check exception here . Right?

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1097 :

Post by admin »

I am sorry but I am not sure I understand your question. Please post the complete code that you have tried compiling and executing.
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests