Question on Exceptions
Hello Everyone,
I have a question related to exceptions?I read K&B but still not very clear regarding programatically thrown exceptions and exceptions thrown by JVM?Are they even remotely associated to checked and unchecked respectively?Is there any other way to understand them other than learning the names by heart?
Thank You
The term exception means "exceptional condition" that will alter the normal flow of program.
When an exceptional event occurs in Java, an exception is said to be thrown.
Exception handler : The code that is responsible for doing something about the exception.
For eg: If you trying to invoke a methond that opens a file and if the file cannot be opened, execution of the method will be terminated, and the code that you wrote to handle that situtation will run.
Some very good extraction I found:
Therefore, we need a way to tell the JVM what code to execute when a certain exception happens.
To do this, we use the try and catch keywords. The try is used to define a
block of code in which exceptions may occur. This block of code is called a guarded region (which really means risky code goes here). One or more catch clauses match a specific exception (or class of exceptionsmore on that later) to a block of code that handles it.
Heres how it looks in pseudocode:
1. try {
2. // This is the first line of the "guarded region"
3. // that is governed by the try keyword.
4. // Put code here that might cause some kind of exception.
5. // We may have many code lines here or just one.
6. }
7. catch(MyFirstException) {
8. // Put code here that handles this Exception.
9. // This is the next line of the exception handler.
10. // This is the last line of the exception handler.
11. }
12. catch(MySecondException) {
13. // Put code here that handles this exception
14. }
15.
16. // Some other unguarded (normal, non-risky) code begins here
Ramki
Message was edited by:
passion_for_java
> I have a question related to exceptions?I read K&B
> but still not very clear regarding programatically
> thrown exceptions and exceptions thrown by JVM?
There's no distinction there as far as your code is concerned.
> Are
> they even remotely associated to checked and
> unchecked respectively?
Nope.
> Is there any other way to
> understand them other than learning the names by
> heart?
Yes. Learn the principles, and learn how to read the docs.
Here's a quick overview of exceptions:
The base class for all exceptions is Throwable. Java provides Exception and Error that extend Throwable. RuntimeException (and many others) extend Exception.
RuntimeException and its descendants, and Error and its descendants, are called unchecked exceptions. Everything else is a checked exception.
If your method, or any method it calls, can throw a checked exception, then your method must either catch that exception, or declare that your method throws that exception. This way, when I call your method, I know at compile time what can possibly go wrong and I can decide whether to handle it or just bubble it up to my caller. Catching a given exception also catches all that exception's descendants. Declaring that you throw a given exception means that you might throw that exception or any of its descendants.
Unchecked exceptions (RuntimeException, Error, and their descendants) are not subject to those restrictions. Any method can throw any unchecked exception at any time without declaring it. This is because unchecked exceptions are either the sign of a coding error (RuntimeException), which is totally preventable and should be fixed rather than handled by the code that encounters it, or a problem in the VM, which in general can not be predicted or handled.
jverda at 2007-7-29 11:45:09 >
