Ignore Exception in C#

Ignore Exception in C#

I don't think there is a trick to avoid exception but you can use the following code snippet:

public void IgnoreExceptions(Action act)
{
try
{
act.Invoke();
}
catch { }
}

Using the method looks like:

IgnoreExceptions(() => foo());

Another solution is to use AOP (Aspect Oriented Programming) - there's a tool called PostSharp which enables you to create an attribute that would catch all exceptions in specific assembly/class/method, which is closer to what you're looking for.

C# Best way to ignore exception

If you are going to just catch, not handle the exception, and ignore it, you can simplify what you have slightly.

try
{
// code
}
catch
{ }

The above is for any exception, if you only want to ignore a certain exception but let others bubble out, you can do this

try
{
// code
}
catch (SpecificException)
{ }

If you do ignore exceptions like this, it is best to include some comment in the catch block as to why you are ignoring the exception like that.

How to ignore exception

try
{
sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);
}
catch (SocketException ex) { }

Ignore an exception in an API controller so that the controller still processes the request

Although, it is a bad design and should never be followed, you can do this workaround:

[HttpPost]
public async Task<ActionResult> ProcessOrder([FromBody] CreateNewOrder order)
{
' *** do stuff with order and process

try
{
SendThankYouEmail(product.Name);
} catch (Exception ex) {
// Swallow the exception by logging it somewhere.
return Ok();
}

return Ok();
}

A simpler way of ignoring specific types of exceptions when awaiting a Task

Yes you can, but you cannot do it using Generics.

If you're willing to pass Types as params, you can do this:

public static async Task<TResult> Ignore<TResult>
(this Task<TResult> task, TResult defaultValue, params Type[] typesToIgnore)
{
try
{
return await task;
}
catch (Exception ex)
{
if (typesToIgnore.Any(type => type.IsAssignableFrom(ex.GetType())))
{
return defaultValue;
}

throw;
}
}

Now, this is much less attractive and you don't have the generic constraint (where TException...) but it should get the job done.

C# Ignoring Exception

Check if the SelectedPath has a value:

void refresh() //gets called by button
{
if(!String.IsNullOrEmpty(objDialog.SelectedPath))
{
listBox1.Items.Clear();

//will cause exception
var files = System.IO.Directory.GetFiles(objDialog.SelectedPath, "*.*", System.IO.SearchOption.AllDirectories);

foreach (string file in files)
{
xxx
}
xxx
xxx
}
}

how to ignore an exception

Currently you're catching all of ComExceptions. If you want to catch other exceptions, you need to provide specific types for each exception.

You can add your exception types after your catch block like this:

    catch (System.Runtime.InteropServices.COMException)
{
MessageBox.Show("Could not initialize Kinect device.\nExiting application.");
_nui = null;

} catch (Exception ex) //this will catch generic exceptions.
{

}

If you want your code to execute after catch No matter what. you can also try to use finally

like this

try
{
//logic
}
finally
{
//logic. This will be executed and then the exception will be catched
}


Related Topics



Leave a reply



Submit