throw an exception in code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #31
    That piece of code doesn't make sense at all. If you want to define your own
    Exception you just extend from the Exception class or the RuntimeExceptio n
    class which both extend from the Throwable class. Check the API documents
    for those classes and please don't try to guess what the syntax would or should
    be for the Java language. My guess is that you either forgot a lot about Java or
    you haven't studied Java much at all before.

    kind regards,

    Jos

    Comment

    • Dlangdaman
      New Member
      • Jun 2007
      • 36

      #32
      I am 100% a novice and never claimed to know fully what Im doing. to politely put me down will not improve the issue now will it?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #33
        Originally posted by Dlangdaman
        I am 100% a novice and never claimed to know fully what Im doing. to politely put me down will not improve the issue now will it?
        That fully contradicts your very first sentence in your original post:

        Originally posted by Dlangdaman
        Its been a while since I have had Java
        My advice remains the same: read Sun's Java tutorials first and don't just guess
        what the syntax and semantics of Java should or would be according to you.

        kind regards,

        Jos

        Comment

        • Dlangdaman
          New Member
          • Jun 2007
          • 36

          #34
          Im trying this and getting Grades.java:39: reached end of file while parsing which I have no idea what that means....thanks again



          Code:
          throw new exception(InvalidGradeException);
                              
                  }
                  for(i = 0;i < grades.length; ++i)
                      
                  {
                      sum += grades[i];
                  }
                  ave = sum / 5.0f;
                  sResult = "The Class average is " + Float.toString(ave);
                  JOptionPane.showMessageDialog(null,sResult);
             } 
              public static void main(String args[])
              {
                  new Grades();
                  System.exit(0);
              } 
              class InvalidGradeException extends NumberFormatException {
              public InvalidGradeException(String s) {
                  super(s);
              }

          Comment

          • Dlangdaman
            New Member
            • Jun 2007
            • 36

            #35
            my first statement says it has been a while since I had JAVA...meaning a class about a year ago with no practice...so that makes me a NOVICE....i am trying to pick this stuff up without guess I just have issues with the formating and use of some items...i am much better with VB

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #36
              Originally posted by Dlangdaman
              Im trying this and getting Grades.java:39: reached end of file while parsing which I have no idea what that means....thanks again



              Code:
              throw new exception(InvalidGradeException);
                                  
                      }
                      for(i = 0;i < grades.length; ++i)
                          
                      {
                          sum += grades[i];
                      }
                      ave = sum / 5.0f;
                      sResult = "The Class average is " + Float.toString(ave);
                      JOptionPane.showMessageDialog(null,sResult);
                 } 
                  public static void main(String args[])
                  {
                      new Grades();
                      System.exit(0);
                  } 
                  class InvalidGradeException extends NumberFormatException {
                  public InvalidGradeException(String s) {
                      super(s);
                  }
              From that tutorial link that I gave you, what did they say was the syntax for throwing an Exception? I've already given you the exception class which you really should have written on your own.

              Comment

              • Dlangdaman
                New Member
                • Jun 2007
                • 36

                #37
                im not sure why everyone is so hostile to help...writing that little bit of code helped me out alot and didnt hurt anyone...i am just trying to improve this app and learn at the same time...Thanks guys

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #38
                  Originally posted by Dlangdaman
                  im not sure why everyone is so hostile to help...writing that little bit of code helped me out alot and didnt hurt anyone...i am just trying to improve this app and learn at the same time...Thanks guys
                  No one is trying to be hostile. We are trying to make sure that you learn this the correct way.
                  There's no point in just giving you all the working code without understanding it.

                  Comment

                  • Dlangdaman
                    New Member
                    • Jun 2007
                    • 36

                    #39
                    i agree...but to show me the working code and where it goes...would give me a working example that I can further develop off of and learn from...at least thats the best way I learn.....Thank s again

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #40
                      Originally posted by Dlangdaman
                      i agree...but to show me the working code and where it goes...would give me a working example that I can further develop off of and learn from...at least thats the best way I learn.....Thank s again
                      You could copy and paste working code and it probably keeps on working while
                      you would have no idea *why* and *how* it works. We're here to make people
                      understand *why* things work and *how* they work; not *that* they work because
                      we already knew that but you didn't. It has nothing to do with hostility.

                      kind regards,

                      Jos

                      Comment

                      • Dlangdaman
                        New Member
                        • Jun 2007
                        • 36

                        #41
                        This Runs and kills the app when you put in an invalid number
                        But What I want is for it to stop and tell the user and then they can continue...how would I do that?
                        can you explain the " SUPER(S)" at the end?

                        Code:
                        import javax.swing.JOptionPane;
                         
                        public class Grades
                        {
                               
                           public Grades()
                            {
                                float grades [] = new float[5];
                                float ave, sum = 0.0f;
                                String students[] = {"Tim", "Mary", "Jen","Derek", "John"};
                                String sgrades, sResult;
                                
                                int i;
                                for(i = 0; i < grades.length; ++i)
                                {
                                    sgrades = JOptionPane.showInputDialog(null,
                                            "Enter the Student's Grade: ", students[i],1);
                                            grades[i] = Float.parseFloat(sgrades);
                                            if(grades[i] > 100 || grades[i] < 0)
                                            throw new InvalidGradeException("Invalid grade.");
                                            
                                }
                                for(i = 0;i < grades.length; ++i)
                                    
                                {
                                    sum += grades[i];
                                }
                                ave = sum / 5.0f;
                                sResult = "The Class average is " + Float.toString(ave);
                                JOptionPane.showMessageDialog(null,sResult);
                           }
                            public static void main(String args[])
                            {
                                new Grades();
                                System.exit(0);
                            }
                           class InvalidGradeException extends NumberFormatException {
                            public InvalidGradeException(String s) {
                                super(s);
                            }
                         
                        }
                        }

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #42
                          Originally posted by Dlangdaman
                          This Runs and kills the app when you put in an invalid number
                          But What I want is for it to stop and tell the user and then they can continue...how would I do that?
                          can you explain the " SUPER(S)" at the end?
                          An Exception carries a message and an optional 'cause' around. The 'cause'
                          can be another Exception which is wrapped in the Exception being thrown.
                          The 'cause' doesn't apply to your Exception but the message does: the 'super(s)'
                          call simply sets the 'message' member to 's'.

                          A Exception being thrown simply aborts the normal flow of execution. If you don't
                          catch it you're 'dead'. If one illegal number entered by the user causes an Exception
                          to be thrown by your own code, you should catch it and do something about it.
                          Please do read the tutorials. The link to the relevant tutorial was supplied to you
                          in a previous reply already.

                          kind regards,

                          Jos

                          Comment

                          • Dlangdaman
                            New Member
                            • Jun 2007
                            • 36

                            #43
                            can you lead me through setting up an exception that give a GUI ok or cancel and then lets them proceed with entering? i have it throwing an exception now but it just kills the program

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #44
                              Originally posted by Dlangdaman
                              can you lead me through setting up an exception that give a GUI ok or cancel and then lets them proceed with entering? i have it throwing an exception now but it just kills the program
                              Let's see what you have then

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #45
                                Originally posted by Dlangdaman
                                can you lead me through setting up an exception that give a GUI ok or cancel and then lets them proceed with entering? i have it throwing an exception now but it just kills the program
                                That's because you don't catch that exception anywere. An uncought exception
                                finally lands at the outmost level, i.e. the jvm itself, which reports the exception
                                and just terminates because the jvm doesn't know what to do with the exception,
                                i.e. it doesn't know how to handle it.

                                Basically your code should look like this:

                                [code=java]
                                try {
                                // your code here that can throw exception X
                                }
                                catch(X x) {
                                // your code did throw exception X; handle it here
                                }
                                [/code]

                                kind regards,

                                Jos

                                Comment

                                Working...