1

I don't quite get the topic "proper exception handling" into my head.

"Exceptions should only be caught only if you can do something to fix that exceptional situation".

I don't understand this. For example: If I do not catch FormatException thrown by Convert.toInt(), even if it's just to show the exception message to the user, my program just crashes. If I had caught this exception and just told the user that the input had the wrong format, it would have survived.

So, should I catch such exceptions or not?

1
  • the program will always crash for UnhandledExceptions Commented Jan 9, 2014 at 7:38

8 Answers 8

4

Exceptions should only be caught only if you can do something to fix that exceptional situation

fixing maybe not the best word here. You should catch exception if you can handle it. Handling may mean:

  • fixing problem or returning some default values
  • retrying something
  • logging or notifying user (I believe every exception should be logged, even if you can fix it)
  • throwing more high-level exception

But you should not catch exceptions and do nothing:

catch(FormatException ex) { } 

That just swallows exception and you will never know if something bad happened.

Sign up to request clarification or add additional context in comments.

Comments

1

It doesn't means that you let the exception unhandled. It means that the proper flow of application is not possible therefore the code should return and notify the caller (an exception or a message). And in your case as the input is invalid therefore you should handle the exception and let the caller know of what is wrong here.

Comments

1

This is a difficult one to answer and it of course depends on your design, application preferences.

When handling exceptions I try to follow these rules:

  • Only catch an exception I can handle and recover from (for example send a notification email). It's possible for the application to continue doing it's task and simply not notify the user - you don't want the process it's notifying to crash simply because your email server is down.
  • Don't program by exception, use a int.TryParse( ) by preference to check rather than relying on exceptions to dictate the program flow.
  • Always catch exceptions before they hit the service boundary. This may be the UI, a WebService or some interface. You should not show a user an exception, instead it should be logged and a pretty user friendly message (or error code) returned to the consumer.

Of course everyone has different opinions on error handling so it's difficult to get a definitive answer. My suggestion would be to build up your own set of rules (unless someone is paying you in which case follow theirs!).

1 Comment

As you can see by the number of answers you've already had - lots of people have strong feelings on this topic!
1

In other words, do not catch exceptions you do not know how to handle. Ultimately any exception should be handled at some point (so your program does not crash) but you should have a sound strategy when and how to do it.

Comments

1

Yes, I think you should catch this exception because actually you can treat it in you catch block by warning the user his input has wrong format.

There's other solutions to avoid the possibility to have a format exception like for example if you ask for an integer and your application's a WPF/Winform/Web application you can use a NumericUpDown control in order to ensure the user enter an integer. Also you can use this kind of code so you won't have to manage exceptions :

if (int.TryParse(userEnteredValue, out resultInt)) { } 

Comments

0

If I do not catch FormatException thrown by Convert.toInt(), even if it's just to show the exception message to the user, my program just crashes.

Right, and this is a case of an exception which can be handled properly. bad input from a user is expected to occur, this, you should handle it.

There are other classes of errors though, things which you can do little about. For example, an OutOfMemoryException.

Comments

0

"Fixing" this situation is not necessarily correcting it. When you catch an error and inform the user about this error, that might be sufficient to "fix" it. E. g. you can let the user correct the input.

Your quote means that you should not do something like this:

try { // do something which can throw } catch(Exception ex) // even this is bad practice as you should try to catch specific exceptions { // do nothing } 

Comments

0

It depends on what the usability you can provide to the user while catching that particular exception.

Let's say you are doing some calculation based on the paramters entered by the user and there is one field that is optional.

Now, if the user enters some string for that field instead of numeric field, your program will crash, if you have not caught exception.

Bu, even if you caught that exception, you calculation will not be completed as you will be skipping the calculation after the exception is thrown.

But what you can do in this situation is, check if FormatException is thrown for that optional field. If yes, ignore the exception using a catch and set the value to some default value, say 0 and then proceed as usual with you calculation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.