Should exceptions be only thrown if I intend to crash the program? [duplicate]

This question already has an answer here:

  • Do exceptions basically exist to prevent a system from crashing? 7 answers

…I wanted to find the source before writing this Q, sadly I couldn’t.

My mental model was that exceptions were thought as an alternative to returning error codes from a function, which is the C way to signalize failure. Returning error codes and managing them is error-prone, cumbersome and tedious; which is why a more right-handed mechanism was thought.

Problem is, then I started reading and hearing opinions that abusing exceptions is bad. Well of course – if I want to see if an array contains element X then it would probably not be a good idea to assume it does and then throw and almost immediatelly catch an exception if it doesn’t.

But these opinions were more strict than that. An example of an improper use of exceptions was if, for example, we have a web app and a user requests something that is not there / sends invalid data / any other condition arises that requires displaying an error page to the user. In this case, I was told, it would be a bad practice to throw an exception deep down in the server code, catch it before the request stops being processed and then display an error page.

If I understand this mentality correctly (which doesn’t have to be the case), the justification was that, since throwing the exception is not intended to crash an app (the situation is recoverable), then the situation is “sensible business-wise”; while exceptions should be reserved for situations “insensible business-wise”, which I suppose means “unrecoverable”? But this seems to imply that if the app can continue and is not supposed to crash, I should not throw exceptions; I should only throw if the app cannot continue and must crash.

Is this commonly agreed upon? If so, then why?

I have feeling this is supposed to apply to almost all programming languages (but, notably, not Python.)

I must admit in my anti-pattern ridden game I do otherwise. For example, when the user sends a JSON with a team, I obviously must validate this data. I seem to be doing precisely what is condemned above: I pass the JSON data to the pieces of code that are responsible for validating it and, if it’s valid, constructing an instantiation of the Team class. If the data is invalid, an exception is thrown. I thought this would be convenient for me, especially since this seems to mix well with C# facilities I use to construct the team: for example, to instantiate a Monster subclass from a monster name present in the JSON, I use Activator.CreateInstance which naturally throws if the monster name present in the JSON doesn’t match any subclass of Monster.

But, well, others are more experienced than me, I marked this with a TODO that I should refactor this to if(isValid(teamJson)) team = new Team(teamJson) even though I don’t understand why and even thought I don’t like this idea because isValid() would have to share a lot of code with the Team() constructor.

Yesterday, however, I read something surprising: Throwing exceptions on assertion failures is, generally, a bad idea, because assertion failure should crash the program while an exception can be caught*. This seems to go against the mentality I presented above, which would assert that exceptions should be thrown precisely when the program must crash.

Could you clear my confusion? When should exceptions be thrown? In particular, should exceptions be thrown then and only then when the program should crash?


*(Yes, this applies to C++ while remarks about my game were about C#; but again – I don’t want to discuss language facilities of this or other language, but when exceptions should be thrown in general, which – again – is AFAIK language-agnostic, although with few exceptions like Python.)

Looping into all websites, PowerShell error: Exception has been thrown by the target of an invocation

I have some powershell script which is looping into all websites in a webapplication. I have full control permissions in the policy in Central Admin. In this powershell loop I got this error at some sites:

PowerShell and Sharepoint: Exception has been thrown by the target of an invocation.

I think this error occurred because I don’t have permissions at some websites. But I would like to skip sites which throw these errors. How can I do that? This is my script:

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $  null) {      Add-PSSnapin "Microsoft.SharePoint.PowerShell"  }   $  webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://portal.myCompany.net")   foreach ($  site in $  webApp.Sites)  {       foreach($  webSite in $  site.AllWebs)      {        # some logic      } } 

Is there a feat like Flick of the Wrist for thrown weapons that does not involve feinting?

Is there a feat like Flick of the Wrist for thrown weapons that does not involve feinting?

Benefit: If you draw a light weapon and make a melee attack with it in the same round, you catch your opponent flat-footed (for the purpose of this attack only). You can use this feat only once per round and once per opponent during any single combat encounter.

Handling an exception thrown in a catch block

If I’m writing a method, in C# for example, I will use a try catch block.

Let’s say the method adds some numbers but I log exceptions in the catch block. What’s the best way to handle an exception causes in the catch block? e.g. the method may have an unrelated exception to the logging in the catch block throwing file not found (maybe file was deleted between file existence check and writing to it).

What’s the best way to handle this and what’s the general behaviour in C#?

Does the Thrown property mean I can attack with my DEX?

On page 194 in the PHB for D&D 5e, it says:

The ability modifier used for melee weapon attack is Strength, and the ability modifier used for ranged weapon attack is Dexterity. Weapons that have the finesse or thrown property break this rule.

So, this means finesse and the thrown property break this rule, which means I can attack with my DEX modifier whenever I use a weapon that has the thrown property. However, it doesn’t seem to be the general opinion. Am I missing something?

This would mean I can attack with a spear or a handaxe using my DEX.

If a dagger thrown at a monk splits into multiple daggers mid-air, each with their own attack and damage rolls, can Deflect Missiles deflect them all?

Situation:

A dagger is thrown at a monk. On that attack, the dagger multiplies into 2 or 3 daggers in mid-air, each dagger with its own rolls for attack and damage. The daggers move one after another such as a line like:

— — —

As a reaction to that one Attack action, can a monk use Deflect Missiles to deflect all of those projectiles, since all of them were thrown on a single Attack action and will be arriving simultaneously?

D&D 5E Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?

For weapons like the spear that have the Thrown property, can a Rogue use sneak attack with them?
Thrown is considered a ranged attack and Sneak Attack specifies that “The attack must use a finesse or ranged weapon.”
Does this mean a rogue could potentially apply Sneak Attack to an attack using a spear as it is technically a ranged weapon? If so, would this also work if you were using the spear in melee?

CSOM: Intermittent Tenant Errors “Exception has been thrown by the target of an invocation.”

We have been seeing intermittent “Exception has been thrown by the target of an invocation.” when querying the tenant this week.

This occurs when either creating a new site via the tenant, or trying to obtain site properties via the tenant using CSOM. The command works one minute, then stops working the next, and fluctuates between working and not working.

Has anyone else been experiencing these issues?

An example of one of the commands which intermittently fails:

SPOSitePropertiesEnumerableFilter filter = new SPOSitePropertiesEnumerableFilter() {     IncludeDetail = true }; var props = tenant.GetSitePropertiesFromSharePointByFilters(filter); tenant.Context.Load(props, p => p.NextStartIndexFromSharePoint); tenant.Context.ExecuteQuery(); 

Invalid DN exception is thrown when creating user in LDAP

I am seeing for certain CN values, when submitted to ldap for user creation, IBM ldap throws the following exception documented in this link indicating invalid Dn syntax. Suspecting if the “CN” values contains commas(,) or equals (=) or any other such special characters that have meaning in LDAP is what may be causing this exception to be thrown. I am not familiar with ldap and need your expertise to confirm if that is the cause. If yes, then what other characters should be avoided in the CN to ensure this DN syntax validation does not fail.