Why do I need to lock the lock?

I have some code as such

try { result.FirstName = nodes[myIdx].Attributes["ows_FirstName"].Value; } catch { } 

Now I don’t know before calling this call if the attribute I'm looking for exists (Good ol sharepoint).

As a result, the only linear way that I can write the code I'm looking for to create as such is.

 try { result.FirstName = nodes[myIdx].Attributes["ows_FirstName"].Value; } catch { } try { result.LastName = nodes[myIdx].Attributes["ows_LastName"].Value; } catch { } .... 

Now I do not use the catch section of this code and get a huge number of lines that are completely redundant.

Why I couldn’t just do

 try { result.FirstName = nodes[myIdx].Attributes["ows_FirstName"].Value; } 

So why are we explicitly forced to declare a catch block, even if it is not being processed? I am sure that there is a good reason, but I can’t solve it.

EDIT: Before everyone starts to leave me, that swallowing an exception is bad, blah blah blah. We (and I) all know these arguments, but in this (and many) real-world scenarios there is nothing exceptional in the exception, and I cannot do (or should not) do anything to correct the behavior.

+4
source share
10 answers

They are not redundant - they have a specific purpose. By default, the absence of a catch will throw the exception into the calling method. The empty catch essentially “swallows” the exception and allows the program to continue, regardless of whether the exception was thrown; usually bad practice.

there is nothing exceptional in exception

It may be true that one type of exception cannot be “exceptional” in this case, but it is not the only exception that can occur. You must handle this exception and treat any other accordingly.

For instance -

What if nodes is null? What if myIdx is outside the nodes array? Any of these conditions would be exceptional, and you must either treat them specifically or let the caller handle them and act accordingly.

[is] nothing I can do (or need to do) to fix the behavior

You may not be able to fix this, but you may need to know this. How does the program behave differently in this case? Record a message? Raise a warning? Set default value? Nothing may be the appropriate answer, but it is very likely that the answer to any possible exception is not appropriate.

+5
source

If you want to catch exception ( catch it does nothing), you must explicitly do this.

This is usually bad practice, so there is no reason to provide a syntax shortcut. Usually you should:

  • Handle the exception in some way. It could mean:
  a.  Retry
 b.  Rethrow it (preserving the inner exception) with a more meaningful message.
 c.  Do it another way.
 d.  Log it (though logging and rethrowing might be better).
 e.  Other

2. Just let it bubble (don't try, or just try / finally).

+6
source

Why not just check for a null element?

 if(nodes[myIdx].Attributes != null && nodes[myIdx].Attributes["ows_FirstName"] != null) { /* ... your code ... */ } 

Or:

 if(nodes[myIdx].Attributes != null) { if(nodes[myIdx].Attributes["ows_FirstName"] != null) { /* ... your code ... */ } if(nodes[myIdx].Attributes["ows_LastName"] != null) { /* ... your code ... */ } } 
+4
source

Perhaps the reasoning is that you should not catch an exception if you cannot handle it. Allowing try without doing the appropriate catch does nothing but use the worst practice.

As for the specific code you are referring to, could you just do a null check before you try to access the index?

+2
source

There must be another way to check if these Attributes exist or not, you should not use exception handling for any functional purpose, you are as follows:

 try { newstring = oldString.ToString(); } catch{} 

what you need to do:

 if(oldString != null) { newstring = oldString; } 

Remember that try catch is designed to handle things named as an “exception”

+2
source

The try / catch block was explicitly designed to trap and handle exceptions that were added to your application.

Simply swallowing Exceptions is usually a bad idea (even if you are just reporting an exception) and should be done explicitly.

+1
source

This is part of the language syntax. You cannot try without at least one catch or finally . There is no stand-alone try , only try-catch , try-finally and try-catch-finally .

It just doesn't make sense to try some code if you are not going to worry about handling the exception ( catch ) or at least make sure that some subsequent code is always executed no matter what happened (which finally ).

+1
source

The main answer in this question was: C #: all exceptions should be caught

In principle, if you catch him, then you should do something with him, otherwise do not catch him. In your case, why can't you check if the attribute value first exists?

+1
source

It looks like you are using one of the SharePoint web services, so is the return type a kind of XmlElement? I am sure there is a way to check if an attribute exists that is cheaper than an exception.

You also need a helper method that encapsulates data validation and retrieval.

+1
source

You should not have empty catch blocks. This is bad programming practice.

Reminds me of a classic ASP On Error Resume Next

0
source

Source: https://habr.com/ru/post/1401511/


All Articles