Ignore Error


Active member
Jun 16, 2015
Programming Experience

I call a class which in turn inherits an API class. That class calls another class which returns a value.

Class1 calls Class2.
Class2 inherits Class3.
Class3 calls Class4 that returns a value. The input to return the value is null. However, Class3 and Class4 are wrapped in an API. So you cant edit it.So it throws an error in Class1. How do I ignore this error?

Your explanation is rather vague about why the exception is thrown in the first place but if you want to catch an exception and carry on then that's exactly what you should do, i.e. catch the exception. That's what try...catch blocks are for. You should do some reading on structured exception handling and try...catch statements in C# but, in general, it's like this:
    // Put the code you want to execute that might fail here.
catch (Exception ex)
    // The tried code threw an exception so perform cleanup here, e.g. log the error.
If any code in the 'try' block throws an exception, execution jumps to the 'catch' block and then continues on after that. It's generally considered bad practice to specify Exception as the type to catch because that will catch any type of exception. You should determine exactly what type of exception is expected and specify that type. That way, exceptions caused by something other than what you're trying to ignore will not be ignored.
If the value (null) is returned to you in class1 without error it appears the problem is in class1, then you can in class1 check if value is null before using to avoid error. It is normally better to avoid error than to ignore it with exception handling.
Top Bottom