Questions tagged [exceptions]

An exception is an occurrence in an application process that requires deviation from the program's normal flow.

493 questions
180
votes
8 answers

Exceptions: Why throw early? Why catch late?

There are many well-known best practices about exception handling in isolation. I know the "do's and don'ts" well enough, but things get complicated when it comes to best practices or patterns in larger environments. "Throw early, catch late" - I've…
shylynx
  • 2,134
118
votes
8 answers

How to write a good exception message

I'm currently doing a code review and one of the things I'm noticing are the number of exceptions where the exception message just seems to reiterate where the exception occurred. e.g. throw new Exception("BulletListControl: CreateChildControls…
Colin Mackay
  • 1,392
79
votes
15 answers

Is it ever ok to have an empty catch statement?

I thought about it and could not come up with an example. Why would somebody want to catch an exception and do nothing about it? Can you give an example? Maybe it is just something that should never be done.
ysolik
  • 6,330
50
votes
9 answers

Throw exception or let code fail

I am wondering if there are any pros and cons against this style: private void LoadMaterial(string name) { if (_Materials.ContainsKey(name)) { throw new ArgumentException("The material named " + name + " has already been loaded."); …
async
  • 854
28
votes
12 answers

Is the 'finally' portion of a 'try ... catch ... finally' construct/structure even necessary?

Some languages (such as C++ and early versions of PHP) don't support the finally part of a try ... catch ... finally construct. Is finally ever necessary? Because the code in it always runs, why wouldn't/shouldn't I just place that code after a try…
25
votes
3 answers

Why is Option/Maybe considered a good idea and checked exceptions are not?

Some programming languages like e.g. Scala have the concept of Option types (also called Maybe), which can either contain a value or not. From what I've read about them they are considered widely to be a superior way of dealing with this issue than…
Mad Scientist
  • 2,826
  • 2
  • 19
  • 19
25
votes
4 answers

How to avoid throwing vexing exceptions?

Reading Eric Lippert's article on exceptions was definitely an eye opener on how I should approach exceptions, both as the producer and as the consumer. However, I'm still struggling to define a guideline regarding how to avoid throwing vexing…
Mike
  • 635
24
votes
10 answers

Parameter to control whether to throw an exception or return null - good practice?

I often come across methods/functions which have an additional boolean parameter which controls whether an exception is thrown on failure, or null is returned. There are already discussions about which of those is the better choice in which case, so…
23
votes
5 answers

Do you throw an argumentexception or argumentnullexception from private methods?

I was just reviewing some code I wrote a while back, and can see that I have a couple of private methods that throw argumentnullexceptions and/or argumentexceptions if there are issues with the methods parameters. I guess my rationale is that helps…
Mr Moose
  • 375
22
votes
4 answers

Exceptions - "what happened" vs "what to do"

We use exceptions to let the consumer of the code handle unexpected behaviour in a useful way. Usually exceptions are built around "what happened" scenario - like FileNotFound (we were unable to find file you specified) or ZeroDivisionError (we can…
10
votes
6 answers

Throwing and catching exceptions in the same function/method

I've written a function that asks a user for input until user enters a positive integer (a natural number). Somebody said I shouldn't throw and catch exceptions in my function and should let the caller of my function handle them. I wonder what other…
usr
  • 201
9
votes
5 answers

Rethrow the same exception to provide more info

Is it a good practice to rethrow the same exception to provide more specific information? For example: var sitemap = "a string containing an XML document"; try { // throw InvalidXmlException if the document is not well formed parse(sitemap); }…
Cequiel
  • 213
9
votes
2 answers

How can I debug exceptions that are not easily reproducible and only occur in a production environment?

I am working on an issue where the exception only occurs in our production environment. I don't have access to these environments, nor do I know what this exception means. Looking at the error description, I'm unable to understand the…
8
votes
5 answers

What does it mean to not catch an exception when we can't handle it?

Can anyone explain what exactly Microsoft means on the below linked page when it says: "Do not catch an exception unless you can handle it and leave the application in a known state." https://msdn.microsoft.com/en-us/library/ms173160.aspx
4
votes
1 answer

Throwing an exception from within a constructor of an exception

As I was TDD-ing some code, it occurred to me that one of my typed exceptions could potentially throw a NullReferenceException while creating its error message. Here's the constructor for the typed exception in question: public class…
CHendrix
  • 514
1
2 3