New answers tagged

2

The main concern I have found with logging close to the source is that you are in danger of logging the same errors twice. In the second case we might imagine someone calling DoSomethingElse() without being sure whether it would log the error. These cases become increasingly common as the size and complexity of the component increases, and the number of ...


1

Main adventage of second solution ( pass errors up ) is: when you run mutiple functions in row that can throw errors, you don't have to make if for each one, but you can just do one if on higher level. And this solution is also encouraged on Go lang blog ( Simplifying repetitive error handling section ).


0

The described scenario is actually a really good fit for doing exception handling correctly -- to stop a process and recover from exceptional errors. Exceptions should bubble up, to the first place where you can continue intelligently, taking a new direction in light of the error. Don't be afraid to let that be all the way out of the application. You can ...


1

Honestly, I would just set up your project so it handles errors gracefully, probably by taking the user to a custom error page (via an error handler) that says something like "Whoops, somthing went wrong. Try again later." I admire the idea of trying to prevent the error in the first place (and @DavidArno's idea with transactions is spot on). But just ...


0

Welcome to the world of coding in an object-oriented language that isn't using a framework to guard against any null-reference pathways. This pain is one of the primary reasons people look into paradigms from other languages to deal with this uncertainty. For example, immutable objects, once instantiated, can safely be treated as never-null if your ...


2

Rather than have your C# code perform all this computation and thus have to handle all the error conditions you describe, why not move it all to a stored procedure that is transactional? That way, you can lock the DB for example, to ensure it doesn't change whilst you perform your multiple steps.


0

They're called "Guard Clauses." It would look something like this: if (!userHasAccess) throw new UnauthorizedException(); if (customer == null) throw new NotFoundException("customer"); if (orders.Count == 0) return 0; if (anOrderHasFailed) throw new NotFoundException("Order " + orderNumber.ToString()); Not that difficult, really, and your code will be ...



Top 50 recent answers are included