8 Replies Latest reply: Jan 8, 2004 9:48 AM by 807582 RSS

    throws exception

    807582
      I have a simple doubt about the use of throws exception in java.Even if i am not throws any exception in method the method will throw the exception right.for eg:
      public void aMethod()throws SQLException will throw sql exception when any sql exception happends in the program.

      public void aMethod() will also will thorw the SQL
      exception when any sql exception happends in the program.


      then ,why should i specify 'throws SQLException'
        • 1. Re: throws exception
          807582
          1.) some compilers or IDE's throw an Exception if a method might throw any exception from outside java.lang - so you'd have to implement this

          2.) it's easier for everyone who should work with your code if he/she knows that a method might throw an exception. so he/she could catch the exception or also throw it... if there is no "throws ..." information each programmer would have to analyse your code to find out if he/she has to catch or throw some Exceptions.
          • 2. Re: throws exception
            807582
            The point of using non-RuntimeExceptions and the Throws clause it to warn the coder that this condition may occur so that they can write code if it happens. Therefore, doing 'throws Exception' has no value, since anything can throw an Error or RuntimeException (or subclass) and 'throws Exception' gives no information about what kind of Exception has occured or what to do about it.
            • 3. Re: throws exception
              807582
              I u write throws exception in some function (to a non runtime exception) then whoever called your function will HAVE to handle that exception by either try/catch or throws in the function definition. the throws will make others who called that function to prepare for a possibliy of an exception and figure a way to handle that possibly in a way they think is best. of course u can make exception with out having to handel them like trying to use a null object but these are run time exceptions (usually) and therefore u cann't catch them.
              • 4. Re: throws exception
                807582
                that's the point ;-)
                • 5. Re: throws exception
                  807582
                  that possibly in a way they think is best. of course u
                  can make exception with out having to handel them like
                  trying to use a null object but these are run time
                  exceptions (usually) and therefore u cann't catch
                  them.
                  You most certainly can catch RuntimeException's
                  • 6. Re: throws exception
                    807582
                    ya but the compiler will not scream if u do not handle and exception from a function that declares that its throwing null exceptions unlike non runtime exceptions
                    • 7. Re: throws exception
                      807582
                      ya but the compiler will not scream if u do not handle
                      and exception from a function that declares that its
                      throwing null exceptions unlike non runtime exceptions
                      hm, i guess that you shouldn't be using NullPointerExceptions in that way?
                      • 8. Re: throws exception
                        807582
                        i know u don't declare throws to runtime exceptions. thats the point i was making. u only handle non runtime exceptions