aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes error in try catch Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "error in try catch" Watch "error in try catch" New topic
Author

error in try catch

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


this gives me a compile error that the try block doesnt throw an ioexception.......i had to remove io from the catch and make it catch(Exception) and then this compiled

but why is this even happening? why does the compiler complain first of all

thanks
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

IOException is a checked exception. So if you catch it, the compiler will make sure that its actually thrown in the body of the associated try block.

Now Exception is also a checked exception. But it has an unchecked subclass RuntimeException. So the compiler will not check if Exception is actually thrown in the body of the try block as the try block might throw a RuntimeException which the compiler is not worried about...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
You mean an ioexception is not thrown in the try block....how is that? i mean when will a try block throw ioexceptions?
Dejan Miler
Ranch Hand

Joined: Nov 14, 2009
Posts: 56
Look in Java documentation.
Method getName do not throw any exception.
IOException will be thrown if you modify you code like this


SCJP 1.6 in progress ....
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: error in try catch
 
Similar Threads
Exception confusion
throws problem
Exception
Help with ArrayList error
Related to Exceptions