question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Internal exceptions being thrown

See original GitHub issue

I updated Spectre.Console and started seeing handled exceptions being thrown, got curious, and traced it back to PR #376. The problem is that while the new try/catch introduced in this PR around the resolver.Resolve(settingsType) call works, it ends up creating an expected control flow where throwing is “normal”. That’s fine as far as it goes, but it does result in unnecessary exceptions under the hood which will both impact performance (though likely not in any critical way) and trigger exception filters when debugging. And it feels icky 😃

The reason I didn’t see these exceptions before the update was because TypeRegistrarExtensions.RegisterDependencies() was registering the settings types in my container for me, so when the old code asked the container for them, they were already in there. That code was removed in this PR so the container doesn’t have the settings type registered any more.

It’s unclear whether this is a “problem” that needs solving. I generally don’t love throwing exceptions during expected flows, even if I’m catching them. If there’s an interest in changing the behavior the best thing I can think of is to introduce an ITypeResolver.TryResolve() that can return a null value for unregistered types without throwing (or alternatively use a bool/out pattern). Then use that to resolve the settings type without a try/catch and fallback to direct instantiation if it returns null.

Would you be interested in a PR for this or should the behavior be considered by design?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
nils-acommented, Nov 14, 2021

@daveaglick not throwing would be the expectation. I tried to make this clearer in https://github.com/spectreconsole/spectre.console/pull/620. I also added the TypeRegistrarBaseTest in Spectre.Console.Testing that can be used by someone implementing an ITypeRegistrar/ ITypeResolver to verify some base expectations of these two.

Closed in https://github.com/spectreconsole/spectre.console/pull/620.

2reactions
daveaglickcommented, Oct 30, 2021

Lol. I like to live on the edge. Nothing like a little pressure!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to automatically call an internal exception ...
I need to execute a method every time a certain custom exception is thrown. The executed code will report the exception to my...
Read more >
Exception Handling in Java
In Java, use the keyword throw, along with the type of exception being thrown. An exception is an object, so it must be...
Read more >
coding style - Throwing an exception inside finally
Basically, finally clauses are there to ensure proper release of a resource. However, if an exception is thrown inside the finally block, ...
Read more >
How to Throw Exceptions in Java
You need to throw exceptions effectively. ... Unchecked exceptions do not need to be explicitly handled; they occur at the time of execution ......
Read more >
Throwing an exception in Java
RuntimeException and its subclasses are so-called unchecked exceptions: they can be thrown at any time, without the method that throws them having to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found