article

Action executed in 0.000

Exceptions

What's wrong with this code? I'm not talking about the indenting style or casing.

If you haven't figured it out by now, it's the catch block. It does nothing. So called "software developer" may understand exceptions, but they don't understand the purpose of catching exceptions.

Let's assume your code or the code you call throws an exception. You have 3 options. (1) handle it, (2) don't handle it, (3) catch it and rethrow an exception.

(1) Let's say you're asking for a person's age. If the user enters "1O" (that's an oh not a zero), this should throw an exception when you convert it to a number. More importantly, the way you handle it is by reprompting, maybe 3 times.

(2) You can outright ignore it. In this case you must declare that you may throw it in the function signature. This is perfectly acceptable. In software, where input is not person-generated, for example reading a CSV file from a client, it's best to let that exception trickle right up through main and out to the developer. This is assuming you don't have to close files or database connections. At least this way YOU KNOW, there is a problem with the data. You can then edit the CSV and repair it. Or if the data is correct you can correct your code.

(3) There are cases, when you are implementing an interface or extending a class, that you can't change the function signature. Eg. public void HttpServlet.doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException. What happens if a NumberFormatException NFE occurs in this function? You must catch it. You should also throw a new ServletException( NFE ). This constructor specifies that NFE is the cause of our servlet exception.

The worst thing to do is, catch the exception and do nothing about it. This will hide errors in your data or your code.

Back to our age example... After 3 times, you've done all you can do to get the age. What should happen after the 4th time? You should catch the NumberFormatException, and throw a new Exception( "User is dumb.", NFE ). Let your callee handle it.

Comments

Too short

Agree, but I would like to see ar real life positive example.
What's wrong is that this example is too short. I have yet to see a nice user input exception handling for a real life database entry screen. Ok, I can throw new Exception( "User is dumb.", NFE ) or maybe better, UserIgnorantlyTriesToEnterNotNumberException but I myself have to handle this puppy too or else it will go screaming up. And the dumb user will not pay me for such software!
parent post: Exceptions
notify me: yes

Post a Comment

* indicates a required field
anonymous (If you want to identify yourself, please sign in first.)
required This field is required.

Max size is 2 MB, aspect ratio 3:4 width:height
required This field is required.
Please include a short description.
required This field is required.

480 characters remaining.
is public

(Use this field if you have to. 3000 characters remaining.)
1 penny, 1 quarter, 2 dimes + 100

Trackback URL

http://derocher.org/~brian//trackback.php?ParentId=1803

form