8 Replies Latest reply on Jul 10, 2001 5:57 PM by 807556

    The difference between an exception and an error

    807556
      As a result of trial and error I found I needed to trap errors on my catch rather than exception. Can anyone enlighten me as to the difference and how I can use both?

      Are all exceptions errors?

      MArk
        • 1. Re: The difference between an exception and an error
          807556
          Do u want catch ur compilation error...

          you can catch only exception....

          • 2. Re: The difference between an exception and an error
            807556
            Hm, not easy.
            I would say exceptions are abnormal behavior, about which you know that it can occur under some circumstances. So you could easily treat an exception by constrains, so try and catch are not necessary. But you use exceptions, because you will spare so the additional tests for the seldom circumstances (e.g. division through zero).
            Errors on the other side are unexpected, you don't know about them until they happen. They are more bugs or errors in design, they are unpredicted. Errors must not necessary throw Exceptions (like Integer.MAXVALUE + 20, which will simply overflow to the negative, making the result wrong = error, but no Exception will be thrown).
            • 3. Re: The difference between an exception and an error
              807556
              I'm not agree with you b_saminathan. You can catch any Throwable object. It isn't yet advisable to catch Error.
              • 4. Re: The difference between an exception and an error
                807556
                I've got some code which was not working with a (I was creating a font and did not have access to XWindows on the Unix box).


                try{

                }catch(Exception e){}

                It works with

                try{
                }catch(Error e){}

                I am trying to understand the difference between an Error object and an Exception. I have seen OutOfMemoryError - I am trying to clarify when something is an error and when an exception...

                Is an Error a problem at the runtime level perhaps??
                • 5. Re: The difference between an exception and an error
                  807556
                  The inheritance tree is like this:

                  Object
                  I
                  Throwable
                  I
                  ------------------------------
                  Error Exception
                  I I
                  --------------- --------------------
                  VirtualMachineException ... RunTimeException....


                  Error are internal messages that JVM sends one another to notify irregularity (OutOfMemoryError, LinkageError...).
                  Exception are classical irregularity (NullPointerException, SecurityException...)
                  • 6. Re: The difference between an exception and an error
                    807556
                    Arrghhh!!
                    Error occured during copy/paste!!! sorry!

                    It's something like this:

                    Object <- Throwable <- Error

                    Object <-Throwable <- Exception
                    • 7. Re: The difference between an exception and an error
                      807556
                      An error is something the current thread shouldn't really be able to recover from but an exception can be recovered in the flow of the program.

                      things which are Errors are:
                           OutOfMemoryError - if the machine ran out of errors, or threads                     couldn't be created etc

                           The program will not function properly if you catch this then it will           happen again a little while later.

                           LinkageError -      this is if there is a problem with the VM                     loading classes, linking to JNI (Native code                     with java interface) calls etc.

                           The program will not function properly because if failed to resolve           a class.

                      As you can see Errors really stop the current thread from achieving its goal, whereas Exceptions.

                      Exceptions are two types aswell, there are checked and unchecked exceptions, checked exceptions are exceptions which must have a try-catch block, whereas unchecked exceptions can be ignored but this causes the thread to stop if it not caught anywhere up the call stack.

                      Unchecked exceptions are exceptions like
                           NullPointerException     -     a value which is used was not                          assigned to a value.

                      This should not really happen if the software was well designed and so java doesn't force you to catch this exception because you shouldn't really cause it in the first place.

                      Checked exceptions are like,
                           MalformedURLException      - if the url is not formed                          correctly.

                      The url most probably could be entered by a user (with some User Interface) of you software and so, this exception is forced to be checked, you are then forced to do something if this happens.

                      Hope this helps.
                      • 8. Re: The difference between an exception and an error
                        807556
                        Oh, you thought about Java Error rather than a general difference.
                        Well, as the docs say:
                        "An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such
                        errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most
                        applications should not try to catch it.

                        A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the
                        method but not caught, since these errors are abnormal conditions that should never occur. "

                        So my statement still persists, Errors a something which is mostly bad design, something unpredicted that should never had happened.
                        So catching Errors can be only a Workaround.