Hi Guys,

I know loads of ppl hv seen similar problems but I just can't figure out whats wrong with my code. I keep getting the following error:

Upgrade.java:133: unreported exception java.lang.Exception; must be caught or declared to be thrown
new SpFwUpgrade(sp, UrlPath);

SpFwUpgrade is called in Upgrade.java as follows:

try
         {
                  new SpFwUpgrade(sp, UrlPath);
                   //new FTPGet(sp,fw,fw,fw);
         }
                catch (STAFException e)
         {
                  return new STAFResult(e.rc, e.getMessage());
         }

SpFwUpgrade.java is written as follows:

public class  SpFwUpgrade{
      public SpFwUpgrade(String sp, String urlPath) throws Exception
     {
       try
        {

           GetFw(urlPath);
          SpFwUpgrade1(sp);

         }
        catch(IOException e){System.err.println(e.getMessage());}
     }

Pls help!!!

Recommended Answers

All 3 Replies

Class SpFwUpgrade throws and Exception, you are catching an STAFFException. Because exceptions are polymorphic meaning they have a hierarchy that means that any other exception that extends the class Exception will be lower in the hierarchy. Therefore you can't expect a STAFFException to work where an Exception is expected. Hope that clears some things up.

You declare "throws Exception" in the declaration of the method, but when you use the method you are catching "STAFException". If "STAFException" is the exception that might be thrown by your method, then you need to declare "throws STAFException" and not "throws Exception". If you do use "throws Exception" then you need to use catch "Exception" when you use the method.

It's as simple as that. You need to "catch" what you "throw". Exactly what you throw (or more (not less) general). I.E. When you declare to throw IOException, you can catch Exception, but not FileNotFoundException (P.S. FileNotFoundException extends IOException which extends Exception).

Edit: a bit slow.

Wow Thanks a ton, that resolved it!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.