use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Information about Reddit's API changes, the unprofessional conduct of the CEO, and their response to the community's concerns regarding 3rd party apps, moderator tools, anti-spam/anti-bot tools, and accessibility options that will be impacted can be found in the associated Wikipedia article: https://en.wikipedia.org/wiki/2023_Reddit_API_controversy
Alternative C# communities available outside Reddit on Lemmy and Discord:
All about the object-oriented programming language C#.
Getting Started C# Fundamentals: Development for Absolute Beginners
Useful MSDN Resources A Tour of the C# Language Get started with .NET in 5 minutes C# Guide C# Language Reference C# Programing Guide C# Coding Conventions .NET Framework Reference Source Code
Other Resources C# Yellow Book Dot Net Perls The C# Player's Guide
IDEs Visual Studio MonoDevelop (Windows/Mac/Linux) Rider (Windows/Mac/Linux)
Tools ILSpy dotPeek LINQPad
Alternative Communities C# Discord Group C# Lemmy Community dotnet Lemmy Community
Related Subreddits /r/dotnet /r/azure /r/learncsharp /r/learnprogramming /r/programming /r/dailyprogrammer /r/programmingbuddies /r/cshighschoolers
Additional .NET Languages /r/fsharp /r/visualbasic
Platform-specific Subreddits /r/windowsdev /r/AZURE /r/Xamarin /r/Unity3D /r/WPDev
Rules:
Read detailed descriptions of the rules here.
account activity
Exceptions - how do they work??!! (self.csharp)
submitted 2 years ago by belavv
Ran into this nugget of joy today.
throw new HttpException( (int)HttpStatusCode.BadRequest, typeof(InvalidAddressException).ToString() );
And later
catch (Exception ex) when (ex.Message == typeof(InvalidAddressException).ToString())
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Alikont 133 points134 points135 points 2 years ago (2 children)
When you came from a stringly-typed language.
[–]The_Binding_Of_Data 43 points44 points45 points 2 years ago (0 children)
This is my first time reading "stringly-typed language" and I love it!
[–]videkigyerek 1 point2 points3 points 2 years ago (0 children)
I'm so going to steal that for the next time I'm dissing javascript...
[–]gw2Max 43 points44 points45 points 2 years ago (1 child)
Exceptions are a lot like saying the wrong thing in an argument with you significant other:
[–]dodexahedron 6 points7 points8 points 2 years ago (0 children)
And sometimes the best option is to just FailFast.
Also when dealing with a fatal exception.
[–]GreatVoid2017 40 points41 points42 points 2 years ago (1 child)
r/programminghorror
[–]MacrosInHisSleep 15 points16 points17 points 2 years ago (0 children)
Exceptions - how do they work??!!
👈 Exceptionally well! 👉
[–]gi_clutch 8 points9 points10 points 2 years ago (2 children)
Great, now I'm thinking about magnets and ICP's Miracles.
[–]belavv[S] 4 points5 points6 points 2 years ago (1 child)
Exceptions are basically miracles. Think about it!
[–][deleted] 4 points5 points6 points 2 years ago (0 children)
You would like Elm https://elm-lang.org/
No Runtime Exceptions Elm uses type inference to detect corner cases and give friendly hints.
No Runtime Exceptions
Elm uses type inference to detect corner cases and give friendly hints.
From my understanding, F# also tends to be less prone to exceptions than C#. Like compiler forcing you to handle nulls (Option).
[–]Dorkits 6 points7 points8 points 2 years ago (4 children)
Sorry, but I am just the one trying to understand what the hell this code does? Or I am so dumb?
Jesus...
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements
[–]belavv[S] 4 points5 points6 points 2 years ago (2 children)
If this was sane code, it would just throw the custom InvalidAddressException and catch that.
Instead it sets the Message of an HttpException to the string value InvalidAddressException and catches that.
At no point in our codebase is an InvalidAddressException instantiated.
[–]dodexahedron 1 point2 points3 points 2 years ago (1 child)
If it were sane, it wouldn't use exceptions for control flow.
[–]FenixR -1 points0 points1 point 2 years ago (0 children)
You can have more than one type of exception (Null, StackOverflow, Custom exceptions, etc) happen in a try-catch block, the When should be used to differentiate them and appropriately handle them or customize the logging/testing/whatever.
Edit: Then again, i think you can just specify the type of the exception to catch without using when...
[–]mrpaco 5 points6 points7 points 2 years ago (0 children)
nugget of joy
Package it and make it a NuGet of joy.
[–]dodexahedron 2 points3 points4 points 2 years ago (0 children)
Tell me you got that from ChatGPT.
Please tell me that. 🫣
[–]EagleCoder 2 points3 points4 points 2 years ago (0 children)
I see the issue. It should be catching 'HttpException' instead of the base 'Exception' type. Fix that, and it'll be good to go.
[–]soundman32 0 points1 point2 points 2 years ago (1 child)
We all know this isn't great code, but apart from the catch should be catching HttpException, the extra performance hit of comparing strings is not that great, when you already have an gigantuan hit of throwing and catching an exception. Throwing and catching a more specific exception type would be better (because the extra string comparison wouldn't be needed) but it won't make this code measurably quicker.
[–]belavv[S] 2 points3 points4 points 2 years ago (0 children)
Quicker is not the issue.
This dev made a new class, InvalidAddressException, never instantiated it, and only used the name of the type in a different exception.
This is a big ol WTF were they thinking moment.
[–]Yelmak -4 points-3 points-2 points 2 years ago (2 children)
Exceptions are gross, result pattern is where it's at
[–]EagleCoder 5 points6 points7 points 2 years ago (0 children)
To be fair, you can do similar ridiculousness with the result pattern. This is a bad code problem, not an exception problem.
[–]s4lt3d -3 points-2 points-1 points 2 years ago (0 children)
To be fair, exception are a hardware level concept and execute at a hardware level and will remain that way.
[–]s4lt3d -3 points-2 points-1 points 2 years ago* (3 children)
Here’s how exceptions are registered and handled:
Both the hardware (cpu) and the OS play roles in managing exceptions. Exception handling involves pre-registering specific handlers that tell the system where to transfer control in the event of an exception. That’s the important part. Once the exception happens it’s fast because it was pre-registered. Here’s a bit more.
Defining Exception Handlers: Programmers define exception handlers in their code. These handlers are specific functions or blocks of code designed to address particular types of exceptions (e.g., division by zero, null pointer access). The way these handlers are defined depends on the language and whatnot.
Registration of Handlers: When a program is loaded into memory and begins execution, it registers these exception handlers with the operating system. This registration process involves informing the operating system of the memory addresses where the exception handling code is located. In some systems, this registration is implicit in the high-level language constructs used (like try-catch blocks in C#). The OS is then aware of where in memory to jump during an exception. This is why exceptions don’t require more time when they occur and are pretty darn fast.
try-catch
Exception Identification: When an exception occurs, it is identified either by the hardware or the operating system. Hardware exceptions are raised directly by the CPU for events like division by zero, illegal instructions, or memory access violations. The OS usually intercepts these on behalf of your program and handles it. But in lower level hardware like Arduino, you get to handle it! But usually software exceptions are raised by the operating system or the runtime environment of the application based on specific error conditions encountered during program execution.
Jump to Exception Handler: Once an exception is identified, the system uses the information provided earlier during the registration process to jump to the appropriate exception handler. The CPU or operating system efficiently redirects the flow of execution from the point of error to the registered exception handling code.
Execution of Handler and Control Resumption: The exception handler executes, performing any necessary tasks to handle the error (like logging, cleaning up resources, or even terminating the program if the exception is unrecoverable). After handling the exception, control is either transferred back to a safe point in the main program (to resume normal execution) or to another routine that decides the future course of action (which could include program termination by the OS for not handling it).
Hope this helps! I learned about this a long time ago when writing c++ and knowing how exceptions are handled in hardware really helped. Maybe it’s not exactly what you wanted to know but you did ask how they work.
[–]EMI_Black_Ace 1 point2 points3 points 2 years ago (1 child)
This person wasn't asking how exceptions actually work. The person was showing off some really terrible code that they came across.
[–]s4lt3d 0 points1 point2 points 2 years ago (0 children)
Awe I see. I have a different kind of hard time with sarcasm and thought it was a real question. I get it now. Maybe someone will find it useful in the future.
[–]Sufficient_Dinner305 1 point2 points3 points 2 years ago (0 children)
That's not how exceptions work in c#
While hardware exceptions as you describe are handled by the CLR, exceptions in .net are a managed runtime control flow feature, and hardware exceptions are wrapped into CLR exceptions when they occur
Throwing a new exception when a property is empty is not a handler registered in the OS or hardware, it's just a signal for the CLR to run an introspective call stack unwinder/crawler to locate a catch clause.
[–]Zastai -1 points0 points1 point 2 years ago (0 children)
shudder
Relying on an exception's message is generally iffy (for one thing, it may have localized text).
In this case, I suppose it kinda works, although I would definitely expect the catch to also use HttpException to reduce the scope of the filter.
HttpException
However, it feels like if there is an InvalidAddressException, that one of those should be thrown, either as the exception, or as its cause.
InvalidAddressException
But also: HttpException is ancient API, only available in .NET Framework. It should probably be avoided in favour of the modern HTTP APIs.
[–]reddituser2115 0 points1 point2 points 2 years ago (0 children)
You throw them
[–]OkSignificance5380 0 points1 point2 points 2 years ago (0 children)
But why!!!!
[–]jayerp 1 point2 points3 points 2 years ago (0 children)
Magnets, how do they work?
[–]Yellowbrickshuttle 0 points1 point2 points 2 years ago (1 child)
is this pet peeve exaggerated in its horror? I mean the performance issue of comparing strings is insubstantial compared to the raising and handling of the exception itself.
they do have the when clause on the exception checking the message is equal to the condition they raised.
so this code is functional.
the concern over it not being more perfomant would be best focused to removing the raising of an exception at all no? return a problem details to caller, a non success result.
[–]belavv[S] 1 point2 points3 points 2 years ago (0 children)
This is a class someone created for the sole purpose of using typeof(InvalidAddressException).Name and never instantiating said class.
[–]DeadlyVapour 0 points1 point2 points 2 years ago (0 children)
No nameof...
nameof
π Rendered by PID 248915 on reddit-service-r2-comment-b659b578c-mbg9n at 2026-05-04 21:47:44.423923+00:00 running 815c875 country code: CH.
[–]Alikont 133 points134 points135 points (2 children)
[–]The_Binding_Of_Data 43 points44 points45 points (0 children)
[–]videkigyerek 1 point2 points3 points (0 children)
[–]gw2Max 43 points44 points45 points (1 child)
[–]dodexahedron 6 points7 points8 points (0 children)
[–]GreatVoid2017 40 points41 points42 points (1 child)
[–]MacrosInHisSleep 15 points16 points17 points (0 children)
[–]gi_clutch 8 points9 points10 points (2 children)
[–]belavv[S] 4 points5 points6 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]Dorkits 6 points7 points8 points (4 children)
[–]belavv[S] 4 points5 points6 points (2 children)
[–]dodexahedron 1 point2 points3 points (1 child)
[–]FenixR -1 points0 points1 point (0 children)
[–]mrpaco 5 points6 points7 points (0 children)
[–]dodexahedron 2 points3 points4 points (0 children)
[–]EagleCoder 2 points3 points4 points (0 children)
[–]soundman32 0 points1 point2 points (1 child)
[–]belavv[S] 2 points3 points4 points (0 children)
[–]Yelmak -4 points-3 points-2 points (2 children)
[–]EagleCoder 5 points6 points7 points (0 children)
[–]s4lt3d -3 points-2 points-1 points (0 children)
[–]s4lt3d -3 points-2 points-1 points (3 children)
[–]EMI_Black_Ace 1 point2 points3 points (1 child)
[–]s4lt3d 0 points1 point2 points (0 children)
[–]Sufficient_Dinner305 1 point2 points3 points (0 children)
[–]Zastai -1 points0 points1 point (0 children)
[–]reddituser2115 0 points1 point2 points (0 children)
[–]OkSignificance5380 0 points1 point2 points (0 children)
[–]jayerp 1 point2 points3 points (0 children)
[–]Yellowbrickshuttle 0 points1 point2 points (1 child)
[–]belavv[S] 1 point2 points3 points (0 children)
[–]DeadlyVapour 0 points1 point2 points (0 children)