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

learn more… | top users | synonyms

7
votes
3answers
338 views

Should I make my own exceptions, or co-opt similar exceptions for slightly non-standard purposes?

This is a general design question, but I'm focusing on C# and .NET because those are the languages I'm working with right now. Should I create my own, new exception classes, or co-opt existing ...
3
votes
3answers
93 views

Is this a valid situation for returning rather than throwing an exception?

This is not something I would ever normally do, but I have a situation where some existing legacy code is being reused in a new application. The code is shared and needs to be used by both the legacy ...
3
votes
4answers
139 views

Design strategy for wrapping exceptions

I'm implementing a type of Repository for a framework/library that has (roughly) the following: public interface FooRepository { boolean contains(String id); Foo fetch(String id); void ...
1
vote
2answers
52 views

Is it valid to expect/throw an exception using a custom caching system?

Imagine some code like the following: class Cache { private Map<String, String> values = new HashMap<String, String>(); public String getFromCache(String key) { if ...
7
votes
1answer
161 views

Do we need to validate entire module usage or just arguments of public methods?

I've heard that it is recommended to validate arguments of public methods: Should one check for null if he does not expect null? Should a method validate its parameters? MSDN - CA1062: Validate ...
4
votes
2answers
202 views

Rust-style error handling in C++

I've been reading some articles on how Rust does error handling using the Result<T, E> type and to me it seems like a hybrid best-of-both-worlds (exceptions and return codes) solution which can ...
2
votes
2answers
70 views

Try Catch and Flow Control dilemma

I am aware that doing Flow Control on a program using a try-catch block is bad practice, but I can't see how to do it in another way when the error caught needs a redirection of the code's execution. ...
3
votes
1answer
64 views

Connection between futures and exceptions?

Is there a connection between futures and exceptions? async-await looks very similar to throw-catch.
2
votes
1answer
31 views

Storing exception information as a member variable for query later, bad practice? [duplicate]

I looking at the best way to handle exceptions, the answer to this question may be to handle the exception in a different place or to not handle to exception at all but to control the flow of the ...
4
votes
2answers
85 views

Strategies to analyze collected exceptions

We want to add error feedback to our application. I had a look at existing solutions (e.g. raygun.io), but these work "in the cloud", which is a no-go for us: most installations of our application ...
1
vote
3answers
102 views

Is an application supposed to recover from a exception thrown in PHP?

What if the programmer uses Exceptions for debugging? Would it be better in that case to just report the failure and immediately interrupt the script since ideally all bugs should be fixed radically? ...
3
votes
2answers
224 views

Best strategy to find the root cause when exception is swallowed by 3rd party

I am sure we have all run into a scenario where a 3rd part API complains with an incomplete stack trace. In essence the error handling in the 3rd party API does something like this, catch ...
2
votes
3answers
143 views

Extended usage of an exception

I'm in the process of building a routing system for learning purposes and have encountered an issue which I think is a bit in the grey area of best practices. Can you guys help me decide if this is ...
-1
votes
1answer
99 views

Should i avoid FaultException for Custom Validation Error messages to client [duplicate]

We are as a team working on a project which currently using WCF as in between client and server. All business rules are written on managers classes which are used by service so its means no ...
0
votes
1answer
80 views

Pass by value result with exceptions [closed]

I have a doubt with the pass-by-value-result method. As far as I understood, it passess the value of the parameter to the function, and then get the result as the function ends. But what happens with ...
173
votes
9answers
12k views

Why do many exception messages not contain useful details?

It seems there is a certain amount of agreement that exception messages should contain useful details. Why is it that many common exceptions from system components do not contain useful details? A ...
3
votes
3answers
382 views

No exceptions C++ and partially constructed objects

Looking over Joint Strike Fighter Air Vehicle C++ Coding Standard, rule AV 73 states something on the lines: Default c++ constructors should be avoided if that means leaving object in a partially ...
7
votes
5answers
450 views

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 ...
4
votes
1answer
143 views

Should I always throw the most specific or should I try to generalize exception types?

Say you normally have FooException. /** * @throw FooException If Foo is invalid for searching. */ public bool exists(Foo a) But at some point you need to have two more specific exceptions for ...
0
votes
4answers
71 views

What's the best way to handle slightly different exceptions?

My code right now looks something like this: void throw_illegal_part_of_input_exception(char c) {} void throw_invalid_input_length_exception(int position, int length) {} void ...
1
vote
1answer
119 views

Using exceptions as regular objects [duplicate]

I have views that handle different errors in my application. For example error 404, 403 and such, giving my errors a more user-friendly presentation. By assumption, they get passed in an exception, so ...
0
votes
2answers
81 views

How do I find out what type of exception I am supposed to raise?

I suppose this question is valid for any programming language that can handle (or at least throw) an exception. However I will stick to Python as an example. The top 2 answers to the following ...
3
votes
1answer
64 views

How to handle Django get-single-instance-in-view pattern?

A lot of my Django views start a little bit like this: try: # here request.POST could also be request.GET or a captured URL parameter MyModel.objects.get(user = request.user, some_attr = ...
4
votes
1answer
115 views

When should I subclass an exception in Python?

In my code there are about seven places where I raise an exception. All of these exceptions are treated the same: print an error to log file, return software state to default and exit. During code ...
0
votes
1answer
125 views

Is there are any common pattern/practice to handle exception list (Java)? [closed]

Suppose I've got a sophisticated parser for something. And a I don't want Throw Early/Fail-Fast strategy. If I've got multiple problems, i want to have list of exceptions in my log file because I want ...
4
votes
2answers
460 views

When to create a custom exception in C#

I'm writing a class to interface with a simple hardware device over a COM port. The device can configured to use various modes, so my class has a SetOperatingMode function, that takes in an enum of ...
0
votes
1answer
77 views

Return values and exceptions [closed]

I wrote simple function that returns a string depending on which condition is TRUE. Here is my code: private String getMyString() { if(!mStrigMember.isEmpty()) { return mStrigMember; ...
4
votes
2answers
318 views

Exception versus return code in DAO pattern

After reading a lot about the abusive use of exceptions in Java and how you should let an exception bubble up through the different layers of an application, I've come to a point where I don't know ...
4
votes
2answers
270 views

Is checking that a property doesn't throw an exception a valid unit test?

I'm writing tests for the following class: public class Foo : INotifyPropertyChanged { private int _failCount; private int _totalCount; public double FailRate { get { double ...
1
vote
2answers
213 views

Smells in exception handling: how to separate between public and internal exceptions?

During the development of a small web API, we decided to separate internal exceptions from public exceptions. Public exceptions are HTTP exceptions, that translate into HTTP responses with proper ...
0
votes
4answers
157 views

Handling DomainExceptions using REST endpoint

Suppose I'm doing some DDD. Now, I have a microservice reflecting a bounded context/a part of a bounded context. Now, suppose there is a REST endpoint: '/somedomainmodel/someaction' My API user is ...
30
votes
8answers
4k views

Is throwing an exception an anti-pattern here?

I just had a discussion over a design choice after a code review. I wonder what your opinions are. There's this Preferences class, which is a bucket for key-value pairs. Null values are legal ...
3
votes
5answers
262 views

Exception handling and 3rd party library

I'm currently having an issue with a 3rd party control library provider. They have an exception occulting culture that gets in the way of my general fail-fast approach when developing software. An ...
0
votes
1answer
165 views

How shall I handle event loop exceptions?

What is the best practice for handling exceptions thrown from event handlers/listeners in a event loop? For example: class EventLoop { public: void start(); //create a thread which calls run(); ...
4
votes
1answer
70 views

Should I use exceptions to control the range of parameters in Python?

For example for the following method signature: def genClusters(n_clusters, n_nodes, cluster_distribution): n_clusters should be an integer of more than 1. n_nodes should be an integer of more ...
51
votes
9answers
6k views

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 ...
1
vote
2answers
153 views

Is it a Good Practice to Catch All Exception? [duplicate]

The snippet below (in java) gives an example of my question. try { // code implementation here } } catch (Exception ex) { } ...
2
votes
1answer
159 views

How to differentiate between exceptions coming from system and business logic?

I would like to differentiate between exceptions coming from business logic like requested database record does not exist attempt to store invalid business data (validation failed) provided CSV file ...
1
vote
2answers
158 views

Recovering a process after illegal instruction exception

Is it possible in theory to recover after a process is mistakenly pointed-out to read from a wrong memory address, rather than terminating it? Let say an error while working with registers lead the ...
1
vote
2answers
319 views

How to Keep Track of Thread Safe Code in a mostly Thread Unsafe Legacy Rich C++ Code Base

For large legacy C++ code bases, notions like Herb Sutter's "const means threadsafe" don't seem to help much, because there can be an overwhelming amount of code in const functions which are modifying ...
0
votes
1answer
115 views

Replacing Throwing Exceptions with Notification in Validations

Martin Fowler recommends replacing exceptions with notifications when dealing with validations. Essentially, instead of throwing an exception, you append error messages to a Notification object that ...
8
votes
2answers
647 views

How should I handle logger failures?

In several of our company's applications, we use a custom logger. It's fairly robust, though we may replace it with something like NLog in the future. One of the logger's tasks is to log any ...
13
votes
4answers
4k views

Why shouldn't a method throw multiple types of checked exceptions?

We use SonarQube to analyse our Java code and it has this rule (set to critical): Public methods should throw at most one checked exception Using checked exceptions forces method callers to ...
2
votes
1answer
83 views

Under which circumstances does it make sense to lose track of where an exception was thrown from?

Is there any valid reason why a catch block on a lower layer would throw back an exception caused by a higher, unknown layer using the following syntax: throw ex; ... rather than: throw; ... ? ...
0
votes
1answer
78 views

Advice needed on rethrowing an exception

Consider the following c# code: public class ExceptionManager { public static void TreatException(Exception ex) { if (ShowAndContinue(ex)) // display a user-friendly ...
38
votes
9answers
6k views

Should I accept empty collections in my methods that iterate over them?

I have a method where all logic is performed inside a foreach loop that iterates over the method's parameter: public IEnumerable<TransformedNode> TransformNodes(IEnumerable<Node> nodes) { ...
3
votes
1answer
143 views

Exceptions in ANSI C

Would it be possible to implement exception mechanism in pure ANSI C similar to the one used in popular OOP languages (with familiar try-catch-finally clauses)? If yes then how? I'm especially ...
1
vote
2answers
922 views

Is it safe to catch ImportError when trying to import optional modules?

I usually see this pattern at least once in every Python project I work on. For example, in a Django project, this is often added at the bottom of the base settings file: try: from .local_settings ...
0
votes
1answer
984 views

Should I throw std::bad_alloc?

I am allocating memory on the stack, and distributing it manually to shared pointers. I end up doing something like this at startup (I've simplified it by ignoring alignment issues): char pool[100]; ...
2
votes
4answers
2k views

How to clean up nested try/except/else?

When writing code, I often want to do something like this: try: foo() except FooError: handle_foo() else: try: bar() except BarError: handle_bar() else: ...