How to handle errors in SharePoint 2010 ?
Error Unhandled exception was thrown by the sandboxed code
wrapper's Execute method in the partial trust app domain:
An unexpected error has occurred.
When you write any custom code, so it is very important to handle an exceptions while development. For custom code, there are many ways to handle the errors in the code.
There are a lot of logging applications are available for .NET and third party tools are there to have logging code ready for implementation on your SP 2010 project for your application.
When you write any custom code for WebParts, then a web part error can cause an entire page to throw an application error in the application and WebPart is important component which should have good handling of exceptions in your application.
So we should not use Try-Catch everywhere in your code rather you can have the service layer in your application architecture.
Issues with Try-Catch-finally block approach:
Generally we use try-catch and finally block to find SharePoint items to find if the item for list, libraries and document items exists or not. Even they dont have items,we dont get any exceptions so its of no use.
When we use ClientContext.ExecuteQuery in our code then it will be executed more than once for Catch block and again in Finally block.
Hence we should use ExceptionHandlingScope so that the lines of code will look much smarter and easier to read with ExceptionHandlingScope.
How to use ExceptionHandlingScope?
ExceptionHandlingScope class ignores an exception that is raised by the server, or executes code in response to an exception that is thrown by the server.
public sealed class ExceptionHandlingScope : IFromJson
Using ExceptionHandlingScope, only one request is sent to the server. Logics in ExceptionHandlingScope are packed together and sent to the server which is executed as a whole in a single request.
Also used for reducing number of calls to the server makes the debugging and modification easier.
So for all SharePoint Developers, ExceptionHandlingScope class is very useful for handling exceptions and errors caused in the application.