Results 1 to 6 of 6
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default Reserved word "throws"

    Hey everyone. I've read many articles about handling exception but some do not discuss throws briefly and others do not discuss em at all.

    I get the idea of try/catch blocks and somewhat of the reserved word "finally."
    What i don't get is why one does not need the catch block when one adds throws Specific Exception or Exception in the main method. What happens when the specified exception occur? Explicitly, what does the reserved word throws do?

    Any help is appreciated. ;)

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    throws is used to tag onto a method signature, that can hint to the caller that this method can throw this exception, but also to enforce that the thing going to call it will need to handle this exception, such as with a try-catch, or in turn its own throws declaration.

    the idea of not needing to catch inside a method sometimes becomes handy if there are a few methods that call other methods in a chain, and when the inner method is not able to do anything anyway, other than to throw an exception that something broke, and would relay on the calling method to do something with it.

    Java Code:
    function firstFunction() {
      try {
        secondFunction();
      } 
      catch (SomeExceptione ex) {
        system.err.println(ex);
      }
    }
    
    function secondFunction() throws SomeException {
      try {
        thirdFunction();
      } 
      catch (SomeException ex) {
        throw ex; // we're just re-trowing it anyway.
      }
      finally {
    
      }
    }
    
    function thirdFunction() throws SomeException {
      // stuff
      //.. oops an error
      throw new SomeException("oops");
    }
    So in this example, the second function, since we are just re-throwing the same exception, we might as well omit the catch () { } part all together.

    Java Code:
    function secondFunction() throws SomeException {
      try {
        thirdFunction();
      } 
      finally {
    
      }
    }
    I do this pattern sometimes with a function that will invoke a database query, because the function that invokes the query, if it fails, i would want the thing that called it to also fail.

    Now, it would be needed if we wanted to not throw the exception, in which case we might not need the 'throws' on the method declaration. Or if we wanted to transform the exception, such as catching a SQLException and then throwing an ApplicationException .

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    OHHH. alrigh i get why we add the throws into a method signature. basically, if you instantiate an obj that has the throws tag in its method header, you have to instantiate it in a try/catch block or else it won't compile.
    I kinda get why we don't need the catch block. btw, for the 2nd function, do we need the the try block in there?

    one more question:
    why do people add "throws SomeException" on to main methods? You can't instantiate a main method. I noticed that if u do add "throws SomeException" in a main method signature, all the objs that have a possibility of throwing "SomeException" don't need to be in a try&catch blocks. Why and how does that work?

    Thanks a ton btw. ;)

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    for the first question, yes you do nteed the try { } thing there, you can't have just a finally { } block on its own without the try { } finally { }. Oddly enough you can't have just try { } on its own either, needs to be at least followed by a catch, or finally.

    you can have a "throws" on the public static void main() method. The main method is special, as the Java VM will invoke it if it exists in the class that was instructed to be ran on the Java VM command line parameters. Having the main() do a throws Exception just saves us from having to write a try .. catch block inside the main method, if there was code that would need to have an exception caught, for java compiler requirements.

    If the main() throws an exception, the Java VM will just print a stack trace (and quit) if an exception is ever encountered.


    One more thing, hoping that its not going to confuse things, there are two main families of exceptions, Checked Exceptions, which are ones that require the throws in the method signature, or a try..catch block to surround them, these are the ones we have been talking about up until now. These extend the java.lang.Exception (or a class that extends that).

    and then there are Unchecked Exceptions, which are exceptions that do not require the throws in the method signature, or a try .. catch. But you could provide one if you choose to. When unchecked exceptions do not have a try catch block, the system will use its built-in handler for catching any unchecked exception. Unchecked exceptions are ones that extend (or classes that extend a class that extends) the java.lang.RuntimeException.

    Some examples of unchecked exceptions include:
    - NumberFormatException thrown when trying to parse a string into a number when this string is not a number
    Java Code:
      int i = Integer.parseInt("steve");
    Here, we do not need to wrap the parseInt() method call with a try catch block, or a throws on the method containing it. If the parseInt()fails, the exception will just work its way up until it encounters something that is able to handle the exception within a catch block. and if your code doesn't have one, the system built in handler will get used.

    But we can at times choose to provide our own handler, such as if we are reading in user input, in this example of parseInt(), we can have it try to to parse a string the user entered and if it fails, catch an exception, show them an invalid number message and cause the loop to read input to repeat to try again.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    damn that is some amazing stuff... I'm going to try summarize your answer and ask if I got the idea:

    For a class to throw Unchecked Exception, the class will need something like "extends SomeException" instead of "throws SomeException" (the throws is for Checked Exception) in its signature.

    So having "throws SomeException" in your method signature be bad programming practice because you're not handling the error yourself?

    Or it's not bad programming practice. It's just that the class gives you the option to handle SomeException however you want.

    I think I'm getting it lol...maybee

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Yes, its about having the option to handle exceptions in different ways.

Similar Threads

  1. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  2. Count lines cointaining "word" in input file
    By gwithey in forum New To Java
    Replies: 5
    Last Post: 04-02-2009, 05:23 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 1
    Last Post: 05-07-2008, 01:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •