Results 1 to 15 of 15

Thread: throws

  1. #1
    jdgallag is offline Member
    Join Date
    Oct 2008
    Posts
    10
    Rep Power
    0

    Default throws

    I have written a program with a method that looks like:

    public method M (type arg, ... ) throws Exception1 {
    ...
    }

    How can I catch this exception? Can I have a different class that only catches exceptions generated by my other classes? An exception agent so to speak?

    like in
    main ...
    ClassThatCausesExceptions c1 = new ClassThatCausesException();
    ExceptionClass ex = new ExceptionClass [c1,...];

    ?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Whatever calls that method catches the exception, or declares itself to throw it, and then whatever calls whatever called that method needs to catch it or declare to throw it, or whatever calls whatever called whatever called it .....

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Look at Sun's tutorial.

    Briefly, every *method* is required to catch any Exception's that can occur within it, using

    try {
    // code that can cause Exception's
    }
    catch (SpecificException ex) {
    // do something about the exception
    }
    finally { //this block is optional
    // some code that *ALWAYS* gets executed
    }

    OR

    the method has to list the Exception's it throws using a "throws" clause.

    Note that I said Exception, which is the base class for all sorts of more specific exceptions. The base interface is Throwable. Along with Exception, there is RunTimeException, which is the base class for many specific exceptions that *do not* have to be listed in "throws" clause but could still occur (like NullPointerException).

    I hope I gave you just enough to get you to read the tutorial ;-)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Adding More: To handle your own exceptions, simply extends that Exception base class.

    Java Code:
    class MyException extends Exception {
         // Public methods to handle exception
    }

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    To handle your own exceptions, simply extends that Exception base class.
    I think Eranga meant to *create* your own exception classes, which is also a good practice, since whoever invokes your methods will know exactly what any exceptions thrown by that method mean.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Great link...

    Here's a link that explains all about exceptions and how to throw them correctly:

    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    classes don't throw or catch exceptions, methods do

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by emceenugget View Post
    classes don't throw or catch exceptions, methods do
    Is anyone talking about this here, or you just want to let it know to our thread starter. :) I thought I've misspell something in my explanation.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Steve11235 View Post
    I think Eranga meant to *create* your own exception classes, which is also a good practice, since whoever invokes your methods will know exactly what any exceptions thrown by that method mean.
    Yes Steve, may be I use the wrong word. :) Thanks for the commenting on my mistake.

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Not a mistake, just a mistranslation...

  11. #11
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i mentioned my post because the writer makes a reference each to both classes creating and catching exceptions, which may be due to misuse of words, but i think it's more due to just not being familiar with what exceptions are and how they work:

    Can I have a different class that only catches exceptions generated by my other classes?

    and

    "ClassThatCausesExceptions c1 = new ClassThatCausesException();"

  12. #12
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It sounds like what you want is the equivalent of a globally registered interrupt handler. That isn't how exceptions work, and the only equivalent to it in Java is a shutdown hook.

    If you want something like that, what you would need to do is create a new Exception (which it looks like you did with 'Exception1'), but have it's constructor register with a global singleton of some kind. The singleton can then be notified of all 'Exception1"s that get created. Then, put a try catch around everything, and convert any Throwables into your new Exception1 class.

  13. #13
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Emcee-

    I think the OP gave up on this post a while ago. Two of us pointed them to the tutorial; hopefully, they took that advice.

    Toadaly-

    That's an interesting idea, having a base exception class whose constructor calls a static method. The problem is, the Exception has to be allowed to work its way through the invocation stack, so every method has a chance to catch it. And, we can't change the definition of Exception, so only home-grown exceptions could be handled that way.

    One way to create a last line of defense is to add the following code around the the logic that drives the rest of the application. Note that this won't do *anything* about other threads that terminate because of an exception; each thread has to be coded this way.

    Java Code:
    try {
      // base logic
    }
    catch (Throwable t) {
      // handle *all* Throwable's, including RuntimeExceptions
    }
    finally {
      // resource deallocations
    }
    Catching Throwable, which is the interface for Exception, guarantees nothing can get through.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by Steve11235 View Post
    Catching Throwable, which is the interface for Exception, guarantees nothing can get through.
    One note though, Be very careful using this type of approach, as RuntimeExceptions, generally, should not be caught. In some instances they can easily be caught and handled (i.e. catching a NumberFormatException when parsing a CSV and expecting an Number when you get some other String, and then aborting the parse, or using a default value if that is acceptable, but keeping the program running) but should not be caught and/or cannot be handled logically/effectively in many others (i.e. a ConcurrentModificationException which indicates a program logic error).

    Edit: Although, if it is a program that runs without direct user interaction, then you should, of course, catch all exceptions to ensure that they get logged properly, but then you should, IMHO, rethrow the caught RuntimeExceptions.
    Last edited by masijade; 02-08-2009 at 10:08 AM.

  15. #15
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I should have stated the obvious when I laid out a global catch:

    All caught exceptions must be fully handled or re-thrown.

    Thanks for pointing that out.

Similar Threads

  1. Exception throws when I read File
    By Juggler in forum New To Java
    Replies: 11
    Last Post: 08-18-2008, 06:09 PM
  2. Replies: 1
    Last Post: 03-28-2008, 05:08 PM
  3. Difference between Throws and Throw
    By Poonam in forum New To Java
    Replies: 7
    Last Post: 02-06-2008, 04:52 PM
  4. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM
  5. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

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