3 Replies Latest reply: Feb 20, 2011 6:31 PM by 793415 RSS

    Exceptionn handling in the constructor

    840446
      The exception handling doesn't seem to be working. This constructor is designed to take numbers (double). When I try to enter a char or a string, the exception handling doesn't work. Does anyone know what I'm doing wrong?

      try{
      Circle c = new Circle('a', "hello", "what");
      }

      catch (Exception e){
      System.out.println("invalid");
      }

      This is the error message that I get:
      Exception in thread "main" java.lang.Error: Unresolved compilation problem:
           The constructor Circle(char, String, String) is undefined

      I just want it to print out the String "invalid" on the screen.
        • 1. Re: Exceptionn handling in the constructor
          EJP
          The exception handling doesn't seem to be working.
          Your expectations are incorrect.
          This constructor is designed to take numbers (double).
          i.e. you have Circle(double, double, double).
          When I try to enter a char or a string, the exception handling doesn't work.
          When you call a constructor that doesn't exist you get a compilation error. Not a runtime exception thrown.
          I just want it to print out the String "invalid" on the screen.
          Impossible as stated.
          • 2. Re: Exceptionn handling in the constructor
            840446
            Thanks!
            • 3. Re: Exceptionn handling in the constructor
              793415
              837443 wrote:
              The exception handling doesn't seem to be working. ..
              No, it is the code compilation that is not working.
              ..This constructor is designed to take numbers (double). When I try to enter a char or a string, the exception handling doesn't work. Does anyone know what I'm doing wrong?
              Please use code tags as described on the 'sticky post' at the top of the forum thread listing.
              try{
              Circle c = new Circle('a', "hello", "what");
              }

              catch (Exception e){
              System.out.println("invalid");
              <li> e.printStackTrace() is shorter and provides much more useful information than the code line above.
              <li> Even if calling printlln, it should arguably done using the System.err stream
              <li> When catching Exceptions, it is best practice to make them as specific as possible. For example, the constructor for new Circle might be expected to throw a FileNotFoundException occasionally depending on user input, but if it ends up throwing a NullPointerException that you never expected, that would indicate a program logic error.
              }

              This is the error message that I get:
              Exception in thread "main" java.lang.Error: Unresolved compilation problem:
                   The constructor Circle(char, String, String) is undefined
              What do you think that means? It seems pretty clear to me. See http://java.syntaxerrors.info/index.php?title=Constructor_undefined for more details.
              I just want it to print out the String "invalid" on the screen.
              Do that before/after dumping the stacktrace.