New answers tagged

0

I would like to answer from the point of view of: I'm facing an InvalidConstraintException What's the logic behind the design of exceptions? And that you are facing a X->Y problem, clearly The exceptions are low-level entities capable of providing minimal information about the failed execution of a block of code. In order to create a meaningful ...


1

This is one of those things where the original intention of the advice is often twisted and misinterpreted through blind preaching, overzealousness or misunderstanding the problem domain. C#'s primary error handling mechanism are exceptions and try pattern. I want to distinguish between errors and exceptions here. Errors are much broader. Exceptions are a ...


1

Many of the other answers are justifying the approach of an existing exception mechanism. That's fine, but it can also be illuminating to compare exceptions to other approaches and ideas. In particular, consider the stack trace attached to an exception. We can generate a stack trace at any point in a program, and it's common to think of them as the history ...


0

It can be helpful to remember that an exception is simply exceptional control flow. Its control flow that was so unusual that it wasn't worth handling right then and there. C was full of that sort of thinking: int err; err = doSomething(); if (err == 0) { // success - keep going } else if (err == 1) { // error: out of memory } else if (err == 2) { ...


1

Most programs have two requirements: Behave usefully when practical. When unable to behave usefully, behave in tolerably useless fashion. In many cases, exceptions are used as a means of meeting the second requirement as cheaply as possible. Having a program report why it is unable to perform its primary task may be somewhat more useful than having it ...


0

The main goal is to avoid cluttering the application with error checking. For example imagine there's an application which writes 100s of different things to disk. Writing to disk is usually successful, and you may not want to have 100s of places in the application which check for a disk error return code. An "exception" solves that problem: the ...


3

There are many kinds of exceptions, so it also matters how you might use that information in this base cases. Exceptions that you expect to catch and handle should absolutely contain all the relevant details about the source that you don't know yet. For example the WebException provides the response that generated it, if it exists, but not the request, ...


2

@ewan's answer is spot on, but I wanted to express this from the performance angle for you. The official Microsoft guidelines mention that exceptions should be avoided for errors that can happen often. I believe the main reason for that advice is indeed for performance. If you follow YAGNI/KISS and other principles, it really doesn't matter, because you ...


4

Exceptions are the way to handle errors. What people mean when they say "don't use exceptions", is "don't use exceptions to handle control flow" So its fine for your payment handler to throw NotEnoughFunds or AccountBlocked, as these presumably hardly ever happen and the action to take is the same. Stop doing stuff and show an error. But ...


2

The Exception object is the base type and encapsulates the barest minimum that an exception needs (including the information that it is an exception in its class name). As the base class, it does well to be as minimal as possible, to not make the implementation of subclasses more difficult than possible, but as powerful as necessary to get all features out ...


2

What matters most is not that you are throwing an Exception. What matters most is that [you hope that] some code, somewhere, will be able to handle that Exception. If all you're doing is popping an "error message" out there for a User to read just before their Application disappears in a Puff of Logic, then any old Exception will do. If, however, ...


-1

To my humble way of thinking, the term, "exception," has always been "a bit of a stretch," because it really describes two very different cases: (1) Not anticipated by program logic: "divide by zero," "square root of negative one." Maybe also: "read past end-of-file," "file does not exist." (2) &...


0

When I am creating an exception (whether it is simply a message that goes into a standard exception or something that inherits from Exception), I consider the same things: what information will be needed when debugging this exception. That means that sometimes I include what variables held what value, and sometimes it’s a more generic InvalidOperation ...


5

Exceptions allow for more elegance in flow-control around both producing errors and handling those errors: Exceptions are more sophisticated than simple error return codes because they un-wind the function call stack until the exception is either handled within a suitable catch block, or the stack fully unwinds and exits the application without being ...


43

"What value is being divided by zero?" is irrelevant - in C#, there is no legal integer value which is allowed to be divided by zero, and even if there were such a value, I can hardly imagine how this information would be useful to find the root cause of this error. The error is caused by the specific value of the divisor, not by the value of the ...


1

TL;DR: I agree, there could be better information in these exception objects. But be careful how you use that information. Exceptions have (at least) a two-fold purpose: To inform the caller that the requested method call failed, so it doesn't uselessly continue and operate on garbage data. To enacpsulate all information useful for the developer or ...


6

The idea is the framework provides a basic set of common problems as exceptions and you, the developer, provide the details in the exception message. It is hard to provide useful, context related information in a generic way. So this is left up to the one who throws or rethrows the exception.


1

Payment processing is a touchy subject. It is very important to have a solution that prevents multiple submits, is resilient to failure, and provides auditability in case of dispute. This is not just for the benefit of your business, but also for compliance purposes, if you take CC payments. To achieve this, your business model should include an entity which ...


2

Work out what different states the system can end up in, e.g. "Not started yet" or "Stripe customer created but no subscription". Store the state for each user in your database. Break down your logic into chunks so that, no matter what state you end up in, you never need to run only part of a chunk. When you process a request, first check ...


Top 50 recent answers are included