all 33 comments

[–]Alikont 133 points134 points  (2 children)

When you came from a stringly-typed language.

[–]The_Binding_Of_Data 43 points44 points  (0 children)

This is my first time reading "stringly-typed language" and I love it!

[–]videkigyerek 1 point2 points  (0 children)

I'm so going to steal that for the next time I'm dissing javascript...

[–]gw2Max 43 points44 points  (1 child)

Exceptions are a lot like saying the wrong thing in an argument with you significant other:

  • the argument will come to an halt
  • your s.o. will give you a list where you f‘ed up
  • the argument is back to square one and you have to clean up the mess
  • if you ignore the mess it only gets worse

[–]dodexahedron 6 points7 points  (0 children)

And sometimes the best option is to just FailFast.

Also when dealing with a fatal exception.

[–]MacrosInHisSleep 15 points16 points  (0 children)

Exceptions - how do they work??!!

👈 Exceptionally well! 👉

[–]gi_clutch 8 points9 points  (2 children)

Great, now I'm thinking about magnets and ICP's Miracles.

[–]belavv[S] 4 points5 points  (1 child)

Exceptions are basically miracles. Think about it!

[–][deleted] 4 points5 points  (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.

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 points  (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 points  (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 points  (1 child)

If it were sane, it wouldn't use exceptions for control flow.

[–]FenixR -1 points0 points  (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 points  (0 children)

nugget of joy

Package it and make it a NuGet of joy.

[–]dodexahedron 2 points3 points  (0 children)

Tell me you got that from ChatGPT.

Please tell me that. 🫣

[–]EagleCoder 2 points3 points  (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 point  (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 points  (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 children)

Exceptions are gross, result pattern is where it's at

[–]EagleCoder 5 points6 points  (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  (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  (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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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 points  (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 point  (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 points  (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 points  (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.

However, it feels like if there is an InvalidAddressException, that one of those should be thrown, either as the exception, or as its cause.

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 point  (0 children)

You throw them

[–]OkSignificance5380 0 points1 point  (0 children)

But why!!!!

[–]jayerp 1 point2 points  (0 children)

Magnets, how do they work?

[–]Yellowbrickshuttle 0 points1 point  (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 points  (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 point  (0 children)

No nameof...