Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what are expected and unexpected exceptions?

Status
Not open for further replies.

kanghao

IS-IT--Management
Jul 4, 2004
68
KR
what is unexpected exception?
what is the counter part of that? maybe expected exception.^^
 
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 !

--------------------------------------------------
Free Database Connection Pooling Software
 
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" :)

--------------------------------------------------
Free Database Connection Pooling Software
 
What is the motivation for your question kanghao?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
and one more. what is the checked exception and what is different from the unchecked exception?
 
I hear you checked exception is the same expected exception.
right?
 
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.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top