86917

Catch and Continue? C#

Question:

Is a simple question that seeks a simple answer. No code is needed as a demonstration. When i call a function it returns an exception and the whole function stops. How can I ignore the exception and continue the function?

Answer1:

You cannot ignore the exception.

If you do not catch it then the exception will propogate up the call stack until somebody does catch it and handle it, or it reaches the top of the call stack and your program halts.

To avoid that, you simply catch the exception and decide how to handle it. If handling it means doing nothing it then simply ... do nothing when you catch the exception:

try { SomeFnWhichThrowsAnException(); } catch { // NO-OP }

The // NO-OP comment (short of "No-Operation") is an indicator I use to indicate that the "handling" of the exception is to <em>deliberately</em> do nothing, to avoid any potential misunderstanding on the part of anyone reading suh code in the future and interpreting an empty catch block as an error or an oversight.

It should be mentioned that even with a comment and a "good reason" to do nothing in response to an exception, this is highly suspect and is a very bad code smell.

It may be more common to specifically ignore very specific exceptions or to do so only in specific circumstances, but to ignore <em>every possible exception</em> is highly unadvisable (consider that this will include exceptions such as stack overflows or out of memory conditions etc).

Answer2:

try { MyFunctionThatErrors(); } catch{}

Answer3:

You can use a try {..} catch {..} statement. Here's the reference docs. <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch" rel="nofollow">https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch</a>

Answer4:

A try...catch statement should do this job:

try { // your code that might throw an exception here } catch { } // code here will execute even if there is an exception

<strong><em>However</em></strong>, try...catch statements are not designed to act as a flow control statement. You shouldn't just ignore the exception without a good reason. You should avoid the exception being thrown in the first place.

For example, Convert.ToInt32 can throw an exception if the string parameter is in the wrong format. You shouldn't use try...catch here as a way to detect invalid user input. You should check whether the input is valid using some other method, like regex for example.

Recommend

  • Renaming a file name to the next incremental number with powershell script [duplicate]
  • Object of type PngImageFile is not JSON serializable
  • Enable MFA Delete on S3 AWS Bucket
  • How to create a share with CloudKit's CKShare?
  • ActiveMQ get queue size without using JMX, JMS
  • Decorate Service in Directive based on Attributes in AngularJS
  • how do I access the object the object i sent to the server file
  • Access denied error while sending proposal to peer from java sdk
  • How do I paginate through a ViewModel in MVC CORE?
  • Storing objects between requests in PHP without serialising
  • Contact image doesn't send when contact saved by CNContact
  • Overriding DefaultDataPath and DefaultLogPath variables when using SqlPackage to publishing a dacpac
  • p:commandButton with p:fileDownload and no ajax only works in second click
  • std::unique_ptr incomplete type error
  • Add new colors along with custom theme colors angular2 material
  • is there any circumstance under which valid C code will not compile properly using g++ [duplicate]
  • VHDL Can you declare a package and an entity in the same file?
  • Cassandra Full-Text Search
  • Find median in four (individually) sorted arrays with O(1) space
  • How to add a Contact in Contact using iPhone Native UI
  • How to recreate a virtual env in python
  • Angular2 DatePipe processing ISO 8601 Date (Invalid argument '*' for pipe 'DatePipe&#
  • Angular xlsx - multiple json to sheet
  • How to handle extraction of double type numbers from a complex input (string) [duplicate]
  • Sphinx4 ConfidenceResult and SpeechResult
  • Firebase reverse dns lookup ENOTFOUND error node.js dns
  • how to make all possible power set(or subset) from arrayList objects?
  • How to gracefully stop python unittest?
  • View.layout() works until next UI update
  • device tree overlay phandle
  • Battery life if using GPS and background app ios/android
  • Create .java file and compile it to a .class file at runtime
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • Ajax call on Multiple selection in Select box
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • Python Flask - GUI for client