Results 1 to 15 of 15
Thread: throws
- 02-05-2009, 02:34 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 10
- Rep Power
- 0
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,...];
?
- 02-05-2009, 02:52 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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 .....
- 02-05-2009, 04:38 PM #3
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 ;-)
- 02-06-2009, 07:41 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Adding More: To handle your own exceptions, simply extends that Exception base class.
Java Code:class MyException extends Exception { // Public methods to handle exception }
- 02-07-2009, 12:14 AM #5
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.To handle your own exceptions, simply extends that Exception base class.
- 02-07-2009, 12:26 AM #6
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-07-2009, 12:42 AM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
classes don't throw or catch exceptions, methods do
- 02-07-2009, 02:01 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-07-2009, 02:02 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 02-07-2009, 02:54 AM #10
Not a mistake, just a mistranslation...
- 02-07-2009, 03:27 AM #11
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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();"
- 02-07-2009, 07:12 AM #12
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-08-2009, 03:16 AM #13
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.
Catching Throwable, which is the interface for Exception, guarantees nothing can get through.Java Code:try { // base logic } catch (Throwable t) { // handle *all* Throwable's, including RuntimeExceptions } finally { // resource deallocations }
- 02-08-2009, 10:04 AM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-11-2009, 01:07 AM #15
Similar Threads
-
Exception throws when I read File
By Juggler in forum New To JavaReplies: 11Last Post: 08-18-2008, 06:09 PM -
Xml Parse throws SaxParseException. Encoding is UTF-8 insteadof ISO-8859-1 ?
By j_kathiresan in forum XMLReplies: 1Last Post: 03-28-2008, 05:08 PM -
Difference between Throws and Throw
By Poonam in forum New To JavaReplies: 7Last Post: 02-06-2008, 04:52 PM -
Main method with throws Exception
By bugger in forum New To JavaReplies: 3Last Post: 01-07-2008, 02:48 PM -
throws Exception
By javaplus in forum New To JavaReplies: 1Last Post: 11-06-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks