• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Catching exception not thrown in try clause

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does below compile fine:



But below gives compiler error "Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body" :



Since both are trying to catch Checked Exception both should give a compiler error.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Exception is a superclass to the unchecked exception such as a RuntimeException. See:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/RuntimeException.html

More about unchecked exceptions and catching or not catching them:
http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html
FileNotFoundException is a checked exception.

The most important message in my opinion is:

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.


 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason was due to java.lang package...

Exception belongs to java.lang package whereas FileNotFoundException belongs to java.io package..

Not only FileNotFoundException even if you use any class ( ex :RuntimeException) belongs to java.lang package ,program will compile without any error...

Because java.lang is the default package recognised by JVM
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Immanuel Marimuthu wrote:The reason was due to java.lang package



J Desai has imported the class java.io.FileNotFoundException so this is not correct. The correct reason as niran said is because FileNotFoundException class has no unchecked exception sub-class but Exception class has an unchecked sub-class which is RuntimeException. This is why if you catch an Exception and even if there's no code throwing any exception in the associated try block, still the program compiles file as the compiler knows that Exception class has an unchecked subclass and the compiler doesn't bothers about unchecked exceptions...
 
J desai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a counter question then why does below fail with error "Unhandled exception type Exception" ?

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is simple, the foo method throws Exception so it needs to be caught. Exception class itself is checked, so you need to catch it in main. It goes like this.

1. The compiler sees that foo method throws Exception
2. The compiler says "Okay so foo method throws Exception, I don't care if it is actually thrown in the body of foo method"
3. The compiler sees that foo method is called from main but there's no try catch
4. The compiler issues you a warning "Hey buddy, looking at the signature of foo method, it seems that it throws a checked exception, so its a part of my job to remind you that you need to handle it"

When you use a try-catch block, and if you catch an Exception, then the compiler does this

1. The compiler sees that you are catching Exception
2. The compiler says "So you are catching Exception, Exception class has an unchecked sub-class RuntimeException, when they hired me they said that I don't have to worry about unchecked exceptions, so I won't check if the exception is actually thrown in the associated try block"

When you use a try-catch block, and if you catch an IOException for example, then the compiler does this

1. The compiler sees that you are catching IOException
2. The compiler says "Hmm, my job is to have my eye on checked exceptions, and IOException has no unchecked subclass, I better look into the associated try block to see if the exception is actually thrown, after all this is a part of my job"
3. The compiler sees that IOException is not thrown in the body of try block
4. The compiler issues a warning "Hey buddy, the try block doesn't throw IOException, so the catch clause is useless, I know this because IOException has no unchecked sub-class, so it was my job to see if its actually thrown or not"

[Edit: changed something misleading for anyone who sees this thread in the future]
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for both it shows the same compile time error, except the exception names:

unreported exception java.lang.Exception; must be caught or declared to be thrown
unreported exception java.io.IOException; must be caught or declared to be thrown

can you clarify, where this is happening

so it was my job to see if its actually thrown or not


in IOException case, it should show a compile error : foo is not throwing IOException, but it shows differently as above ?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all I wrote something wrong in my previous post, if a method declares any checked exception in its throws clause, then the body of the method is not required to throw that exception. This is compulsory in cast of try-catch block.

So if a try-catch block catches any exception class which is not an unchecked exception itself and doesn't has any subclass which is an unchecked exception, then the body of the try block must actually throw that exception somewhere, otherwise you'll get a compilation error.

Following the above, IOException is neither an unchecked exception, nor does it has any subclass which is an unchecked exception. So if a catch block catches IOException, then the code in the try block must actually be throwing IOException somewhere otherwise you'll get a compilation error saying that IOException is never thrown in the try block.

In case of Exception class, Exception has an unchecked subclass i.e. RuntimeException. So if a catch block tries to catch Exception, then the compiler will not check the try block to see if Exception is actually thrown in the method or not.


Coming to your question Garlapati, if a method declares a checked exception in its throws clause, then the calling code MUST handle that exception by either declaring that exception in its own throws clause, or using a try-catch block to catch it. Having an unchecked subclass doesn't matter in this case...
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason behind J Desai ' s code and as mentioned by Ankit, that since Exception class has both Checked Exception and Runtime Exception as subclasses, and since there is nothing in the try block, so the compiler gives it a pass thinking the try block can have Runtime Exception which it bdoes not need to catch. Not so for the Unchecked Exception case. It tries to search for one, does n't get it and so consequently cannot enter the catch block.
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if an exception is a checked exception or has any subclasses that are checked exceptions and some are unchecked exceptions....then why does the compiler give that the given exception must be declared to be thrown or caught???

the compile doesnt complain that the exception isnot thrown when it has a unchecked subclass exception then why does the compiler complain when thhat exception is thrown without catching it decalring it to be thrown?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:if an exception is a checked exception or has any subclasses that are checked exceptions and some are unchecked exceptions....then why does the compiler give that the given exception must be declared to be thrown or caught???

the compile doesnt complain that the exception isnot thrown when it has a unchecked subclass exception then why does the compiler complain when thhat exception is thrown without catching it decalring it to be thrown?


Can you explain your doubt more clearly, I'm unable to understand what you are trying to say here...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you said that since Exception has a checked Exception subclass and an unchecked exception subclass we can use a try block without actually throwing an exception in the try block since the comipler knows that a runtime exception can be thrown so it doesn check.......but my question is then why do we have to provide a try catch block in the first place....when the compiler allows in the first case since exception has an unchecked exception and since compiler doesnt check for unchecked exceptions...?

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think wehave to put throw new Exception() in the try block because it has an checked subclass exception...so the methoid might throw a check exception and we have to catch it to decalsre it to be thrown........i hope this logic is right?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:you said that since Exception has a checked Exception subclass and an unchecked exception subclass we can use a try block without actually throwing an exception in the try block since the comipler knows that a runtime exception can be thrown so it doesn check.......but my question is then why do we have to provide a try catch block in the first place....


Okay so what I understood is that you are trying to say that why do we need to catch RuntimeExceptions (I'm not talking about all unchecked exceptions here). The answer is, you don't need to catch RuntimeExceptions. RuntimeExceptions can be avoided by using good programming practices. Lets say that I have an application which asks the user for his/her name. I want to check that the user has entered a name. So what I do is this

The above code can be improved like this

This way you can avoid RuntimeExceptions. Coming to general unchecked exceptions, there are many cases where we need to catch them, we are not forced by the compiler, but we need to catch then for our program to run smoothly. For example if I ask the user for his/her age, then my code would look something like this

Here the parseInt method can throw NumberFormatException is not a checked exception. Still I'm catching it, because if I don't, my program will crash if the user doesn't enter a numerical age, and I don't want my application to crash for such a simple reason...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude my question was why do we need to catch Exception...that is new Exception()
Exception has both checked and unchecked and unchecked are not checked by the compler so why do we need to catch new Exception().....

a while ago you said that a compiler allows that you do not throw new Exception() in a try block because it has both checked and unchecked exceptions....

and i wrote this maybe because the program might throw a checked exception which has to be caught or declared to be thown

leave if you didnt understand what i said...

 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:Exception has both checked and unchecked and unchecked are not checked by the compler so why do we need to catch new Exception().....


I'm assuming that you were trying to say throw new Exception() (as new Exception() is just instantiating Exception class and if its actually not thrown, there's no question of catching it). Exception has both checked and unchecked sub-classes. But Exception itself is a checked exception. So if you throw it, then you need to catch it. Basically the thing is, you always have to catch any checked exception that is thrown (or declare the checked exception in the throws clause). Now coming to the other side of the question, if you catch a checked exception, it must be thrown in the body of the associated try block. There are two checked exception classes that defy this rule. These are Exception and Throwable. Both are checked exception classes, but if you catch them, then they don't need to be thrown in the body of the associated try block (because both have unchecked sub-classes). Still if I'm not answering what you are trying to ask, then you can further clarify your doubt or someone else might answer it if they understand your question (generally it helps to understand a question if you provide a simple piece of code with it)...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got you now...but is it true that every class that has both unchecked exceptions and checked exceptions has subclasses will itself be called a checked exception
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju wrote:is it true that every class that has both unchecked exceptions and checked exceptions has subclasses will itself be called a checked exception


Again I couldn't understand you properly, but if a class itself is a checked exception class, then its sub-classes would be checked exceptions. And if a class itself is unchecked exception class, then its sub-classes will be unchecked exceptions. So since Exception class itself is checked exception, so any sub-class that we create of Exception class would be checked exception. RuntimeException is unchecked exception, so any sub-class that we create of Exception class would be unchecked exception...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude but isnt Runtime Exception a subclass of Exception class?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:dude but isnt RuntimeException a subclass of Exception class?


Yes it is, still Exception is checked and RuntimeException is unchecked. java.lang.RuntimeException and java.lang.Error are special classes which are unchecked even though they derive from checked exception classes (Exception and Throwable respectively). If you look at my post again, I said any exception class that we create from a checked exception class will be checked and any exception class that we create from unchecked exception class will be unchecked...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh.....thanks for being so patient and making me understand...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic