Search code examples
c#throw

'ApplicationException' could not be found


Printscreen of my code with the error and my references. There is an error with the 'ApplicationException', and I just don't know how to resolve it.

using System;
using System.Net;
using System.Net.Http;


namespace Sharepoint_2013_REST_API
{
   public class Program
   {
       public void Main(string[] args)
        {
            //Init
            string baseURL = "hello";
            string uriString = "world";

            System.Net.Http.HttpClient _Client = new System.Net.Http.HttpClient();
            _Client.BaseAddress = new Uri(baseURL);
            HttpResponseMessage resp = _Client.GetAsync(uriString).Result;
            string respString = resp.Content.ReadAsStringAsync().Result;
            if (resp.StatusCode != HttpStatusCode.OK)
            {
                throw new ApplicationException("BAD");
            }
        }
    }
}

Error notification:

Error CS0246 The type or namespace name 'ApplicationException' could not be found (are you missing a using directive or an assembly reference?) 25


Solution

  • The ApplicationException class isn't available in Portable Class Libraries and ASP.NET vNext projects, which I think your project is.

    More importantly:

    You should derive custom exceptions from the Exception class rather than the ApplicationException class. You should not throw an ApplicationException exception in your code, and you should not catch an ApplicationException exception unless you intend to re-throw the original exception.

    So use Exception when you throw an exception.