Best practice for throwing exceptions in an utility class - c#

I am creating a utility class that will be used in my Facebook application for tasks that are commonly done, such as retrieving a Facebook Page ID from a URL. I am unsure if the below code is the correct way to throw and catch exceptions. Could someone please advise, thanks.
Utility Class:
public static class FacebookUtilities
{
public static string GetPageIDFromGraph(string pageUri, string accessToken)
{
try
{
FacebookClient client = new FacebookClient(accessToken);
dynamic result = client.Get(GetPageIDFromUri(pageUri), new { fields = "id" });
return result.ToString();
}
catch (FacebookOAuthException)
{
throw;
}
catch (FacebookApiException)
{
throw;
}
}
public static string GetPageIDFromUri(string pageUri)
{
if (pageUri.Contains('/'))
pageUri = pageUri.Substring(pageUri.LastIndexOf('/') + 1);
if (pageUri.Contains('?'))
return pageUri.Substring(0, pageUri.IndexOf('?'));
else
return pageUri;
}
}
Program class, just testing:
- Note "input" and "output" are just textboxes.
private void btnGetPageID_Click(object sender, EventArgs e)
{
try
{
output.Text = FacebookUtilities.GetPageIDFromGraph(input.Text, "Some Access Token Goes Here");
}
catch (FacebookOAuthException ex)
{
if (ex.ErrorCode == 803)
{
output.Text = "This page does not exist";
}
}
catch (FacebookApiException ex)
{
if (ex.ErrorCode == 100)
{
output.Text = "The request was not supported. The most likely cause for this is supplying an empty page ID.";
}
}
}
Is it correct to simply rethrow the exception from the utility class so that the calling class can catch it and do what needs to be done?

It seems that you do nothing with catched exceptions - so dont catch them. There are a lot of discussions about exception handling, but in general you should catch exceptions when you have something to do with them, or at least using finally to clean up resourses.

Since you aren't handling the exceptions in any way, your code can just be:
public static string GetPageIDFromGraph(string pageUri, string accessToken)
{
FacebookClient client = new FacebookClient(accessToken);
dynamic result = client.Get(GetPageIDFromUri(pageUri), new { fields = "id" });
return result.ToString();
}
You should only catch exceptions when you can meaningfully handle them, and it doesn't look like you can in your GetPageIDFromGraph method, so you should just propagate them.

Related

What is the best practice for handling Try Catch when writing automated testing?

I'm currently writing test automation for a web based application for my company. I am utilising C#, Visual Studio test suite, and Selenium to perform testing.
Today I asked the question to my colleague of 'Is there any time where there is too many Try Catch blocks in code?'. His reply was to not work as I am at the minute (see example 1), but to just get the lower level try-catch to throw to the upper level try-catch so that the exception can be written there and the test failed (see example 2).
Example 1:
TestCase.cs
[TestMethod]
public void TestLogin()
{
Assert.IsTrue(FW_Shared.Perform_Login(FW_Shared.OrgCode, FW_Shared.Username, FW_Shared.Password));
Console.WriteLine(#"Login Successful");
}
FW_Shared.cs
public static class FW_Shared
{
public static string OrgCode = "Test123";
public static string Username = "Tester";
public static string Password = "Password";
public static void Perform_Login(string OrgCode, string Username, string Password)
{
try
{
Driver.Url = "http://test.app.com/";
Driver.FindElement(By.Id("org_code")).SendKeys(OrgCode);
Driver.FindElement(By.Id("username")).SendKeys(Username);
Driver.FindElement(By.Id("password")).SendKeys(Password);
Driver.FindElemenet(By.Id("btnsubmit)).Click();
}
catch (Exception ex)
{
Console.WriteLine(#"Error occurred logging on: " + ex.ToString());
return false;
}
return true;
}
}
Example 2
TestCase.cs
[TestMethod]
public void TestLogin()
{
try
{
Assert.IsTrue(FW_Shared.Perform_Login(FW_Shared.OrgCode, FW_Shared.Username, FW_Shared.Password));
Console.WriteLine(#"Login Successful");
}
catch (Exception ex)
{
Console.WriteLine(#"Exception caught, test failed: " + ex.ToString());
Assert.Fail();
}
}
FW_Shared.cs
public static class FW_Shared
{
public static string OrgCode = "Test123";
public static string Username = "Tester";
public static string Password = "Password";
public static void Perform_Login(string OrgCode, string Username, string Password)
{
try
{
Driver.Url = "http://test.app.com/";
Driver.FindElement(By.Id("org_code")).SendKeys(OrgCode);
Driver.FindElement(By.Id("username")).SendKeys(Username);
Driver.FindElement(By.Id("password")).SendKeys(Password);
Driver.FindElemenet(By.Id("btnsubmit)).Click();
}
catch (Exception)
{
throw;
}
return true;
}
}
Now I know that throwing the Exception to be caught is generally useless in typical coding as you want to handle specific exceptions that are returned, but I want to be able to catch any general web page or element issues so the test can fail on a general issue with the web application. For instance:
If the web page returns 503 or 404 issues
If an element is not present on the current web page
If an elements name has changed.
There is points in testing other, more complicated parts of the application that I handle unaccessible parts/elements with true/false bool returns and assert that, but since I am referencing multiple function across different classes would sticking with what I have be best, moving to top-level catching of all lower exceptions, or should I be doing something else?
I normally like to create a test helper method in its own class called "TestRunner.cs" which has a method in I use for all the tests which might throw an exception and I want to test the results of e.g.
public static Exception RunCodeThatMayThrowException(Action action)
{
try
{
action.Invoke();
return null;
}
catch (Exception ex)
{
return ex;
}
}
I can then just use the method like:
// Act
var actualException = TestRunner.RunCodeThatMayThrowExeption(() => {//some code});
// Assert
//Do some asserts
Your original Perform_Login method states no return parameter (‘void’) and always returns ‘true’ (unless there’s a crash) – these both hint that the method needs some re-factoring.
I would re-factor as follows, which isolates the calling code from the exception, makes the caller agnostic to any errors in the called method, and avoids a series of try-catches passing up the call stack (if your test had a try-catch in it then the production code that calls the method may well require the same):
Public Static Class FW_Shared
{
public static string OrgCode = "Test123";
public static string Username = "Tester";
public static string Password = "Password";
public static bool Perform_Login(string OrgCode, string Username, string Password)
{
Try
{
Driver.Url = "http://test.app.com/";
Driver.FindElement(By.Id("org_code")).SendKeys(OrgCode);
Driver.FindElement(By.Id("username")).SendKeys(Username);
Driver.FindElement(By.Id("password")).SendKeys(Password);
Driver.FindElemenet(By.Id("btnsubmit)).Click();
}
Catch (Exception ex)
{
Console.WriteLine(#"Error occurred logging on: " + ex.ToString());
return false;
}
return true;
}
}
[TestMethod]
Public Void TestLoginSuccess()
{
Assert.IsTrue(FW_Shared.Perform_Login(FW_Shared.OrgCode, FW_Shared.Username, FW_Shared.Password));
Console.WriteLine(#"Login Successful");
}
[TestMethod]
Public Void TestLoginFailure()
{
Assert.IsFalse(FW_Shared.Perform_Login(FW_Shared.OrgCode, “foo”, “bar”));
Console.WriteLine(#"Login Failed");
}

Handling exception in task

I'm new to TPL.
I need to handle exception when the SendEmailAlert() method throws any error.Is the following code correct please?
public Task MyMethod()
{
DoSomething();
try
{
string emailBody = "TestBody";
string emailSubject = "TestSubject";
Task.Run(()=> SendEmailAlert(arrEmailInfo));
}
catch (AggregateException ex)
{
ex.Handle((e) =>
{
log.Error("Error occured while sending email...", e);
return true;
}
);
}
}
private void SendEmailAlert(string[] arrEmailInfo)
{
MyClassX.SendAlert(arrEmailnfo[0], arrEmailnfo[1]);
}
I forced an error from within SendEmailAlert() method.But the exception is not getting caught. Could someone advise?
Thanks.
Your Task.Run runs in a different context (you would need a try/catch inside it; or check if the task is done). You could change to use async/await.
Example:
public async void MyMethod()
{
try
{
await ExceptionMethod();
}
catch (Exception ex)
{
// got it
}
}
public async Task ExceptionMethod()
{
throw new Exception();
}

Handling custom exception classes

I am developing a MVC application.
I am trying to define my own exception classes. I am making this structure first time...
What else I have to add in my exception classes , like constructor or something , so it work well... ?
public ActionResult Edit(EmployeeVM employeeVM)
{
EmployeeService employeeService = new PartyService();
try
{
PartyService partyService = new PartyService();
partyService.Update(PartyVM);
}
catch (DuplicateEntityExcpetion e)
{
TempData["error"] = e + "Party you are trying to add is already exist.";
}
catch (InvalidDataException e)
{
TempData["error"] = e + "Data which you are trying to add is not valid.";
}
public class InvalidDataException : Exception
{
}
public class DuplicateEntityExcpetion : Exception
{
}
}
You must throw these exceptions from your service so that they get catched here.
throw new DuplicateEntityExcpetion("Your Query or Message");
throw new InvalidDataException ("Your Query or Message");
Also, it is a good practice to have a generic exception handled so that you have taken care of all exceptions.
catch (Exception e)
{
TempData["error"] = e + "Data which you are trying to add is not valid.";
}

Reusing try catch for wcf call

I have a series of methods that call wcf services and all of them have the same try catch code
Response Method1(Request request)
{
Response response = null;
using(ChannelFactory<IService1> factory = new ChannelFactory<IService1>(myEndpoint))
{
IService1 channel = factory.CreateChannel();
try
{
response = channel.Operation(request);
}
catch(CommunicationException ex)
{
// Handle Exception
}
catch(TimeoutException ex)
{
// Handle Exception
}
catch(Exception ex)
{
// Handle Exception
}
}
return response;
}
And so on (I have 6 methods like this for different services).. how can i encapsulate all the service calls and handle the exceptions in a single method
EDIT
Following Nathan A's advice I created a simple generic method:
protected TResult ExecuteAndCatch<TResult>(Func<T, TResult> serviceCall, T request)
where T : Request
where TResult : Response
{
try
{
return serviceCall(request);
}
catch (CommunicationException ex)
{
}
catch (TimeoutException ex)
{
}
catch (Exception ex)
{
}
return null;
}
The new methods would like this
Response NewMethod1(Request request)
{
Response response = null;
using(ChannelFactory<IService1> factory = new ChannelFactory<IService1>(myEndpoint))
{
IService1 channel = factory.CreateChannel();
response = channel.Operation(request);
}
return response;
}
and i'm trying to call it like
Response response = ExecuteAndCatch<Response>(NewMethod1, new Request())
What am I doing wrong?
Use a wrapper function.
Take a look at this article: http://mytenpennies.wikidot.com/blog:writing-wcf-wrapper-and-catching-common-exceptions
Here's an example from the article:
private void ExecuteAndCatch<T> (Action<T> action, T t) {
try {
action (t);
Success = true;
}
catch (TimeoutException) {
Success = false;
Message = "Timeout exception raised.";
}
catch (CommunicationException) {
Success = false;
Message = "Communication exception raised.";
}
}
If your client derives from ClientBase<T> e.g MyClient : ClientBase<IWCFService>
You could then create your own base class that provides methods that will wrap the common functionality.
The below sample code could be expanded to allow the final derived class to specify what to do when a particular method call fails. Here I just call HandleError
In specific client class
//method that returns a value
public int Ping()
{
return Protect(c => c.Ping());
}
//void method usage
public void Nothing(int stuff)
{
Protect(c => c.Nothing(stuff));
}
In client base class
protected void Protect(Action<IWCFService> action)
{
Protect(c => { action(c); return true; });
}
//add other exception handling
protected Protect<T>(Func<IWCFService, T> func)
{
try
{
return func(Channel);
}
catch (FaultException e)
{
HandleError(e);//up to you to implement this and any others
}
return default(T);
}
inject the various clients through an interface and then run the operation in a single place?
HttpResponse performOperation(IServiceClient injectedServiceClient)
{
IServiceClient client = injectedServiceClient;
try
{
client.Operation();
}
catch(CommunicationException ex)
{
// Handle Exception
}
catch(TimeoutException ex)
{
// Handle Exception
}
catch(Exception ex)
{
// Handle Exception
}
return httpResponse(httpStatusCode.OK);
}

What is the best practice in C# to propagate an exception thrown in a finally block without losing an exception from a catch block?

When an exception is possible to be thrown in a finally block how to propagate both exceptions - from catch and from finally?
As a possible solution - using an AggregateException:
internal class MyClass
{
public void Do()
{
Exception exception = null;
try
{
//example of an error occured in main logic
throw new InvalidOperationException();
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
try
{
//example of an error occured in finally
throw new AccessViolationException();
}
catch (Exception e)
{
if (exception != null)
throw new AggregateException(exception, e);
throw;
}
}
}
}
These exceptions can be handled like in following snippet:
private static void Main(string[] args)
{
try
{
new MyClass().Do();
}
catch (AggregateException e)
{
foreach (var innerException in e.InnerExceptions)
Console.Out.WriteLine("---- Error: {0}", innerException);
}
catch (Exception e)
{
Console.Out.WriteLine("---- Error: {0}", e);
}
Console.ReadKey();
}
I regularly come into the same situation and have not found a better solution yet. But I think the solution suggested by the OP is eligible.
Here's a slight modification of the original example:
internal class MyClass
{
public void Do()
{
bool success = false;
Exception exception = null;
try
{
//calling a service that can throw an exception
service.Call();
success = true;
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
try
{
//reporting the result to another service that also can throw an exception
reportingService.Call(success);
}
catch (Exception e)
{
if (exception != null)
throw new AggregateException(exception, e);
throw;
}
}
}
}
IMHO it will be fatal to ignore one or the other exception here.
Another example: Imagin a test system that calibrates a device (DUT) and therefore has to control another device that sends signals to the DUT.
internal class MyClass
{
public void Do()
{
Exception exception = null;
try
{
//perform a measurement on the DUT
signalSource.SetOutput(on);
DUT.RunMeasurement();
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
try
{
//both devices have to be set to a valid state at end of the procedure, independent of if any exception occurred
signalSource.SetOutput(off);
DUT.Reset();
}
catch (Exception e)
{
if (exception != null)
throw new AggregateException(exception, e);
throw;
}
}
}
}
In this example, it is important that all devices are set to a valid state after the procedure. But both devices also can throw exceptions in the finally block that must not get lost or ignored.
Regarding the complexity in the caller, I do not see any problem there either. When using System.Threading.Tasks the WaitAll() method, for example, can also throw AgregateExceptions that have to be handled in the same way.
One more note regarding #damien's comment: The exception is only caught to wrap it into the AggregateException, in case that the finally block throws. Nothing else is done with the exception nor is it handled in any way.
For those who want to go this way you can use a little helper class I created recently:
public static class SafeExecute
{
public static void Invoke(Action tryBlock, Action finallyBlock, Action onSuccess = null, Action<Exception> onError = null)
{
Exception tryBlockException = null;
try
{
tryBlock?.Invoke();
}
catch (Exception ex)
{
tryBlockException = ex;
throw;
}
finally
{
try
{
finallyBlock?.Invoke();
onSuccess?.Invoke();
}
catch (Exception finallyBlockException)
{
onError?.Invoke(finallyBlockException);
// don't override the original exception! Thus throwing a new AggregateException containing both exceptions.
if (tryBlockException != null)
throw new AggregateException(tryBlockException, finallyBlockException);
// otherwise re-throw the exception from the finally block.
throw;
}
}
}
}
and use it like this:
public void ExecuteMeasurement(CancellationToken cancelToken)
{
SafeExecute.Invoke(
() => DUT.ExecuteMeasurement(cancelToken),
() =>
{
Logger.Write(TraceEventType.Verbose, "Save measurement results to database...");
_Db.SaveChanges();
},
() => TraceLog.Write(TraceEventType.Verbose, "Done"));
}
As the comments have suggested this may indicate "unfortunately" structured code. For example if you find yourself in this situation often it might indicate that you are trying to do too much within your method. You only want to throw and exception if there is nothing else you can do (your code is 'stuck' with a problem you can't program around. You only want to catch an exception if there is a reasonable expectation you can do something useful. There is an OutOfMemoryException in the framework but you will seldom see people trying to catch it, because for the most part it means you're boned :-)
If the exception in the finally block is a direct result of the exception in the try block, returning that exception just complicates or obscures the real problem, making it harder to resolve. In the rare case where there is a validate reason for returning such as exception then using the AggregateException would be the way to do it. But before taking that approach ask yourself if it's possible to separate the exceptions into separate methods where a single exception can be returned and handled (separately).

Categories