What an Exception Is

     

An exception is some bad news that you get told about during the execution of your program. There are many reasons for this. Your database could be down, and then the application that is trying to connect to it would not be able to do what it wants. Network problems are common causes of exceptions. Your program could rely on a file that cannot be found, or a user could enter something that is just ridiculous. You have to deal with these problems because they happen.

An exception is one result of poorly written code. If you write quick and dirty code, you should expect to have problems later. A good example of this is, oh, anything written in VB (which explains the comparatively large number of VB programming jobs on the planet).

An exception is different from an error. An error is something that you can't do much about. That is, your program probably would not be able to recover from an error at all. This is something like you're completely out of memory. In some circles, it is considered pedantic to make a distinction between exceptions and errors, and you'll commonly see the two terms used interchangeably. In the interest of preserving what little is left of our collective sanity , let's just call it six of one, half dozen of the other.

An exception is something that you can create to take care of an ailing situation, and reroute the flow of the application.

An exception is something that you throw , or that the runtime itself throws. Throwing means that you make it available again to another part of the program, make it bubble up the call stack ”it means you.

An exception can be part of the method signature. You can write, "I know that I'm doing potentially dangerous stuff in this method, but I do not want to deal with anything bad that happens. I am going to pass the buck back up to the method who called me." Here's how you do that:

 

 public void someMethod(int someID) throws            SQLException { ... } 

An exception is an instance of a java.lang.RuntimeException or java.lang.Error, or an instance of a class that extends one of them. Runtime exceptions are also called unchecked exceptions .

An exception that you create is an instance of a java.lang.Exception, which implements the java.lang.Throwable interface.

When an exception occurs, you should deal with it if you can.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net