All exceptions are the results of two main problems :
1) Bad coding - doing something with a variable that you do not expect.
2) OS level errors that stop your code functioning correctly.
I don't really believe there are actually a difference between "expected and unexpected". If you are a programmer - you should never perform an operation that you do not fully understand the many threads of execution.
However, perhaps philosophically you could kind of distinguish.
For example, an "expected error" could be one such as an IOException when loading a file. Everyone knows that an IO exception could easily be thrown - so we all prepare for it.
However, what about parsing numbers ? If you have a String that is usually "1", this will parse using Integer.parseInt() easily. However, a common newbie mistake is to perform a parseInt() on a String that is "1.1". This will throw a NumberFormatException - an could be interpreted as being "unexpected".
But I don't really believe in this - its just a level of skill and knowing how to code correctly.
Now, one possible real "unexpected exception" is OutOfMemoryError - there is not a lot a java programmer can do to guard against failure to malloc space on the heap - because we don't work directly with heap allocation.
But mostly, no exception should be unexpected - and if it is, you need to code better !
I would say that an unexpected exception is one that you didn't know a method could throw. By definition, this would therefore have to be one that subclasses RuntimeException or Error, since these do not have to be declared to be thrown.
Tim
--------------------------- "Your morbid fear of losing,
destroys the lives you're using." - Ozzy
... there are some exceptions which are thrown by the JVM when something bad goes wrong internally. These sublass Error and no-one can reasonably expect them to happen.
Tim
--------------------------- "Your morbid fear of losing,
destroys the lives you're using." - Ozzy
True, one cannot guard against runtime errors really ... but being pedantic here, these kind of runtime errors are not exceptions - they are JVM internal problems - not "thrown exception"
An expected exception is java.io.EOFException, if you don't know how long a file is, you read.
Getting rid from all exceptions by 'proper coding' is Illusion.
If you test for example a SQL-connection, before using it, it may get corrupted immediately after your test (someone cut's the wire).
If you test a String for being a valid integer before trying to convert it by 'Integer.parseInt (s)', you will produce a lot of code (probably wrong code), duplicating what is already done by 'Integer.parseInt (s)'.
I guess it was a good idea to make NumberFormatException an unchecked Exception, because sometimes you can guarantee, that it must not be thrown.
Unexpected is an exception, which is so generally, that it may occur every time (out of memory i.e.). You may not expect it at a specific point.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.