----- Original Message -----
Sent: Friday, August 06, 1999 8:36
AM
Subject: Strange Question
Hi,
I have a strange
question. How hard is to the server to handle Exceptions ?
To be more precise here is an example:
I have a form with 3 fields:name,telephone,address.
I retrieve the parameters in the servlet with the usually
method req.getParameterValues("name")[0].
Now if the servlet is called from the right form that
everything is OK.
But if someone removes the 'name' field from the form my
servlet throws a
NullPointerException wich is a normal
behavior.
So my question is: this exception will be a SHOCK to the
server(or the JVM) or it will terminate the
servlet as nothing were happen?
Or another question: Too many exceptions will
produce an
abnormal behavior of the server(JVM) after a time
??
I am asking this question becuase if the answer is
NO (meening that Exceptions don't affect at all
the server or JVM) that i will drop the extra if
conditions like:
if(req.getParameterValues("name")!=null)
name=req.getParameterValues("name")[0].
Waiting for Your answers.
Thank you,
Andras
Hi Andras,
Defensive design of software
is a large, often tiresome part of the job. If there is any chance of a
servlet dying because of
missing parameters you should take account of
this risk and check for null values when getting parameter values. You can
then
take appropriate action like warning the user of
this problem in an orderly way, setting parameter placeholders to sensible
values etc.
There are always unforeseen circumstances that
can effect the running of a servlet over which you can control however you
needn't worry
too much about those.
If you treat your design like a fortress and
build your defenses early on you will find that you will have less nasty
surprises as your software
goes about its business later.
Golden rules of thumb...
1 - Take nothing for granted, if it can go wrong
it will at some point.
2 - Take corrective action rather than react to a
problem, this saves you having to worry about
forseeable exception, as
you do with your if statement above.
3 - To avoid your servlets or JSPs being called
inappropriately check the request object for the referrer, ie the
link
that was clicked to
bring the client there. If it is not an expected value send a redirect to
somewhere sensible.
There are plenty more of these rules of thumb but
none more general than the 3 above, they work for me perhaps
you will find them useful.
Andy Bailey