Linked Questions

10
votes
6answers
1k views

What to return if something failed, rather than doing something 'random'? [duplicate]

Say I re-implement a method for finding the position of a string within a string. So I get either the position which is an integer, or a "magic number" like -1. I see this pattern in so many ...
-3
votes
2answers
847 views

Why ever use exception throw (in C#) except for Class Library development? [duplicate]

Why would I ever throw exceptions in code? (except for one specific scenario where I am developing a class library which the consumers of it, will not want / be able to change). To be more specific, ...
0
votes
2answers
132 views

How should I handling throwing exceptions in C#? [duplicate]

I'm developing a game engine in C# and I just had a question about handling exceptions correctly. Since the engine will be used by other .NET developers, how should I handle exceptions? What I mean ...
1
vote
1answer
127 views

API for a function, should I throw exception? [duplicate]

I have to create a method CalculateMean(int[] x) If the input is an empty array, should I return 0 (or some number), or throw exception?
1
vote
1answer
94 views

Use the chance to return booleans after method calls for an optional layer of exception handling? [duplicate]

I would like to ask a follow-up to a question I just asked: Better to have 2 methods with clear meaning, or just 1 dual use method? I now understand why it is best to separate charge(float c); and ...
161
votes
8answers
47k views

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 ...
22
votes
5answers
19k views

Function returning true/false vs. void when succeeding and throwing an exception when failing

I'm building an API, a function that uploads a file. This function will return nothing/void if the file was uploaded correctly and throws an exception when there was some problem. Why an exception ...
24
votes
10answers
3k views

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 ...
10
votes
4answers
961 views

Reworking a function returning an integer code that represents many different statuses

I've inherited some awful code that I've included a short sample of below. Is there a name for this particular anti-pattern? What are some recommendations for refactoring this? // 0=Need to log in / ...
4
votes
2answers
1k views

Should a Connect() Method return false on failure or throw exception?

I found this question Return magic value, throw exception or return false on failure?, however it is kind of broad. I would like a more specific answer. Say I have a method called Connect() which ...
1
vote
5answers
657 views

Avoiding exceptions for performance optimization

In our code base, I see a lot of code like this var error = ValidatePhoneNumber(userId, phoneNumber); //if validation fails, return error if(!string.IsNullOrEmpty(error)) { return error; } If I ...
0
votes
2answers
641 views

Exception Handling: When and Why?

The main languages I use are C++ and Java. Both languages support exception handling. I confess, I may not actually understand exception handling, at least, I certainly don't understand why you ...