Click here to Skip to main content
15,946,316 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
QuestionException or Return Value? Pin
Richard Andrew x6423-Jun-24 10:31
professionalRichard Andrew x6423-Jun-24 10:31 
AnswerRe: Exception or Return Value? Pin
Greg Utas23-Jun-24 15:30
professionalGreg Utas23-Jun-24 15:30 
It may depend on your application and whatever is standard practice in your existing code base, if you're dealing with one.

For a system that handles lots of transactions, I strongly advise using an exception only if a transaction should be aborted because of a serious error that also gets logged for debugging. The exception gets caught well down the stack, where bulletproof recovery and logging are initiated. In this type of system, it is very rare for application code to catch exceptions.

The general problem with exceptions is that the caller must know that a function will throw. Even if this is documented by keywords or comments, will the catch actually get written? Nothing forces it to be, whereas it's harder to ignore an error code. And if the catch isn't written, the exception trickles down the stack. What can anyone else make of it? Ultimately it's up to that bulletproof code unless crashing is OK.

Exceptions also have more overhead than error codes, not only in CPU time but in code bulk.

There's also the question of where the finger of blame should point. If someone passes your function a null pointer and you use it, guess where the SIGSEGV occurs? So it's your code that ends up looking crappy, even if the stack trace can point you to the real culprit. I therefore make it a habit to check arguments for validity and to generate a log and return an error code if one is found to be nonsense. This also makes debugging easier for everyone.

It'd be hard to convince me that a system should use exceptions other than sparingly. Having a system that mixes error codes and exceptions seemingly at random makes no sense.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.

AnswerRe: Exception or Return Value? Pin
Mircea Neacsu23-Jun-24 15:34
Mircea Neacsu23-Jun-24 15:34 
GeneralRe: Exception or Return Value? Pin
trønderen24-Jun-24 3:45
trønderen24-Jun-24 3:45 
GeneralRe: Exception or Return Value? Pin
Mircea Neacsu24-Jun-24 6:15
Mircea Neacsu24-Jun-24 6:15 
AnswerRe: Exception or Return Value? Pin
Richard Deeming23-Jun-24 21:36
mveRichard Deeming23-Jun-24 21:36 
Questionhow should I plan my learning trajectory? Pin
Prudence Clearwater22-Jun-24 9:34
Prudence Clearwater22-Jun-24 9:34 
AnswerRe: how should I plan my learning trajectory? Pin
Richard MacCutchan24-Jun-24 6:14
mveRichard MacCutchan24-Jun-24 6:14 
GeneralRe: how should I plan my learning trajectory? Pin
Prudence Clearwater26-Jun-24 17:58
Prudence Clearwater26-Jun-24 17:58 
GeneralRe: how should I plan my learning trajectory? Pin
Richard MacCutchan26-Jun-24 22:32
mveRichard MacCutchan26-Jun-24 22:32 
GeneralRe: how should I plan my learning trajectory? Pin
Ruby Harris15-Jul-24 20:07
Ruby Harris15-Jul-24 20:07 
QuestionBest way to send files to a central location Pin
gthp_cp7-Jun-24 2:15
gthp_cp7-Jun-24 2:15 
QuestionIs a separate view model relevant for Blazor server side given that we can test the whole component with bUnit? Pin
Mr Yossu25-Feb-24 5:38
Mr Yossu25-Feb-24 5:38 
AnswerRe: Is a separate view model relevant for Blazor server side given that we can test the whole component with bUnit? Pin
Gerry Schmitz25-Feb-24 10:06
mveGerry Schmitz25-Feb-24 10:06 
GeneralRe: Is a separate view model relevant for Blazor server side given that we can test the whole component with bUnit? Pin
Mr Yossu26-Feb-24 3:37
Mr Yossu26-Feb-24 3:37 
GeneralRe: Is a separate view model relevant for Blazor server side given that we can test the whole component with bUnit? Pin
Gerry Schmitz26-Feb-24 4:43
mveGerry Schmitz26-Feb-24 4:43 
GeneralRe: Is a separate view model relevant for Blazor server side given that we can test the whole component with bUnit? Pin
Mr Yossu26-Feb-24 4:49
Mr Yossu26-Feb-24 4:49 
AnswerRe: Is a separate view model relevant for Blazor server side given that we can test the whole component with bUnit? Pin
jschell26-Feb-24 12:32
jschell26-Feb-24 12:32 
AnswerRe: Oh my goodness! Pin
Dave Kreskowiak24-Feb-24 4:38
mveDave Kreskowiak24-Feb-24 4:38 
GeneralRe: Oh my goodness! Pin
Gerry Schmitz24-Feb-24 4:42
mveGerry Schmitz24-Feb-24 4:42 
GeneralRe: Oh my goodness! Pin
Dave Kreskowiak24-Feb-24 5:53
mveDave Kreskowiak24-Feb-24 5:53 
QuestionC# How to populate datagridview from a separate combobox selection Pin
mourad barsoum22-Feb-24 12:57
mourad barsoum22-Feb-24 12:57 
AnswerRe: C# How to populate datagridview from a separate combobox selection Pin
Gerry Schmitz23-Feb-24 7:40
mveGerry Schmitz23-Feb-24 7:40 
AnswerRe: C# How to populate datagridview from a separate combobox selection Pin
Vrend Top11-Apr-24 1:13
Vrend Top11-Apr-24 1:13 
GeneralRe: C# How to populate datagridview from a separate combobox selection Pin
RedDk11-Apr-24 8:11
RedDk11-Apr-24 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.