Topic
5 replies Latest Post - ‏2013-12-31T13:45:13Z by gsager
Codeoline
Codeoline
141 Posts
ACCEPTED ANSWER

Pinned topic Unhandled exception type ClassNotFoundException

‏2013-12-20T15:40:06Z |

I have a method in my LJO that will checks a table in the DB for a serial number.  This routine throws ClassNotFoundException  and SQLException's.  I am calling this routine from a method builder and would like to know how to add throws to method builders.

Thank You

This is the LJO routine i am calling

    public static boolean getSerialNo(WebAppAccess webAppAccess, String serialNumber) throws ClassNotFoundException,SQLException {
        boolean returnResult = false;

        String selectSQL = "select unit from PAR where unit = '" + serialNumber + "'" ;

          try {
            CallDB2 callDB2 = new CallDB2();
            Class.forName(callDB2.getDB2Class());
            Connection con = DriverManager.getConnection(callDB2.getDB2Url(webAppAccess));
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(selectSQL);
            while (rs.next()) {
                returnResult = true;
                }
            con.close();
        }
        catch (ClassNotFoundException e) {
    //        System.out.println(e);
            throw e;
        }
        catch (SQLException e) {
//            System.out.println(e);
            throw e;
        }
        return returnResult;
    }

This is how it is being called in the method builder

  {
    boolean returnResult = false;
    Variables variables = webAppAccess.getVariables();
    PageAutomationMessages errors = (PageAutomationMessages)variables.getObject("viewForm_InputPageError");
  

    IXml values = variables.getXml("serviceConsumerGetSerializedPartsListInputs");
    String serialNumber = values.getText("serialNumber");
    
    SerializedPartsListItemManager serializedPartsListItemManager = new SerializedPartsListItemManager();
    returnResult = serializedPartsListItemManager.getSerialNo(webAppAccess, serialNumber);
    
    
    System.out.println(serialNumber);
    
    }

This is the error message

An error has occurred.
The error message returned was: "---------- 1. ERROR in C:\IBM\Web Experience Factory\WASCE\repository\geronimo\SerializedPartsList\1\SerializedPartsList-1.war\WEB-INF\factory\generated\genjava\Consumer\_SerializedPartsListConsumer.java (at line 175) returnResult = serializedPartsListItemManager.getSerialNo(webAppAccess, serialNumber); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unhandled exception type ClassNotFoundException ---------- 2. ERROR in C:\IBM\Web Experience Factory\WASCE\repository\geronimo\SerializedPartsList\1\SerializedPartsList-1.war\WEB-INF\factory\generated\genjava\Consumer\_SerializedPartsListConsumer.java (at line 175) returnResult = serializedPartsListItemManager.getSerialNo(webAppAccess, serialNumber); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unhandled exception type SQLException ---------- 2 problems (2 errors)".

  • gsager
    gsager
    131 Posts
    ACCEPTED ANSWER

    Re: Unhandled exception type ClassNotFoundException

    ‏2013-12-26T19:09:36Z  in response to Codeoline

    Since getSerialNumber is a static public method why are you not just calling

    try{

    SerializedPartsListItemManager.getSerialNo(webAppAccess, serialNumber);

    }catch(Exception e){

    // if you need to re-throw e here then or handle  the error

     throw e;

    }

     

    without creating a new object?  And y

  • mburati
    mburati
    2551 Posts
    ACCEPTED ANSWER

    Re: Unhandled exception type ClassNotFoundException

    ‏2013-12-27T04:19:12Z  in response to Codeoline

    As Garry mentioned if it's a static method, you should just try calling it instead of constructing a new object first.

    Also, as he mentioned, it's best to try to catch exceptions and handle them in your code where you know what the app is doing and what it should do on error, rather then let it just bubble up to something else that may not know those details.

    If you do need to rethrow it however,  since it looks like it's not a runtime exception, you may want to wrap it in one to avoid having to declare the method builder as throwing that lower level exception.  The following WEF utility exception class lets you wrap an exception in a runtime exception (via constructor): http://infolib.lotus.com/resources/portletfactory/8.0.0/WEF80ABD003/en_us/api/com/bowstreet/util/WrappedException.html

    One other ugly workaround that may also work if you have to rethrow an exception is to declare your method throws that exception by placing the throws clause before the open curly brace in the method builder.   A cleaner workaround would be to put your code in an LJO Java class, rather than a method builder, where you can declare it to do whatever you need it to do.

    I hope that info helps,
    ..Mike Burati 
    The postings on this site are my own and do not necessarily represent the positions, strategies, or opinions of IBM.

     

    • Codeoline
      Codeoline
      141 Posts
      ACCEPTED ANSWER

      Re: Unhandled exception type ClassNotFoundException

      ‏2013-12-30T16:28:30Z  in response to mburati

      I changed my method builder code as you suggested. The problem I am having now is that when it goes into the LJO method it is not making it past the connection statement.  If i disable the post save and call getSerialNo from another method it has no problem with the connection statement.  Have I done something wrong? Your help is always appreciated.

      Please find the updated code in the method builder below.

       {
            boolean returnResult = false;
             Variables variables = webAppAccess.getVariables();
          PageAutomationMessages errors = (PageAutomationMessages)variables.getObject("viewForm_InputPageError");
          IXml values = variables.getXml("serviceConsumerGetSerializedPartsListInputs");
          String serialNumber = values.getText("serialNumber");
          serialNumber = serialNumber.toUpperCase();
          String serial_Error = errors.getMessage("SERIALNUMBER_ERROR");
      try{
          returnResult = SerializedPartsListItemManager.getSerialNo(webAppAccess, serialNumber);
             
      }catch(Exception e){
            if (!returnResult)  
                  errors.addMessage("SERIALNUMBER_ERROR", "Serial number not found. " );
      }
      }

       

      • Codeoline
        Codeoline
        141 Posts
        ACCEPTED ANSWER

        Re: Unhandled exception type ClassNotFoundException

        ‏2013-12-30T20:56:13Z  in response to Codeoline

        After doing some testing I figured out that if i hard coded my DB information and disabled my Import to XML properties builder that is in my provider it works fine.  Is there a way i can get this to work with Import to XML properties builder?

        • gsager
          gsager
          131 Posts
          ACCEPTED ANSWER

          Re: Unhandled exception type ClassNotFoundException

          ‏2013-12-31T13:45:13Z  in response to Codeoline

          What is the XML properties builder referencing?

          Is the XML file you referenced deployed to the server?

          I had a situation that the property file I referenced was in the project but was excluded by the deployment configuration, because the name was in the .excludeFromServer file.  So the model built fine but couldn't find the properties on the server since the properties were not on the server.