【问题标题】:'ApplicationException' could not be found找不到“应用程序异常”
【发布时间】:2016-02-22 19:52:10
【问题描述】:

Printscreen 我的代码有错误和我的参考。 “ApplicationException”有一个错误,我只是不知道如何解决它。

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");
            }
        }
    }
}

错误通知:

错误 CS0246 找不到类型或命名空间名称“ApplicationException”(您是否缺少 using 指令或程序集引用?) 25

【问题讨论】:

  • 你能构建 sln 吗?
  • 不,'构建失败'
  • 取决于您的目标框架,例如,它在 .NETCore 中不可用。从通用项目模板开始将是你的糟糕之处。 ApplicationException 是一个设计错误,只需使用 Exception 代替。并确保您确实打算使用通用模板,它看起来不是很通用。

标签: c# throw


【解决方案1】:

ApplicationException 类在 Portable Class Libraries 和 ASP.NET vNext 项目中不可用,我认为您的项目是。

更重要的是:

您应该从Exception 类而不是ApplicationException 类派生自定义异常。不应在代码中抛出 ApplicationException 异常,也不应捕获 ApplicationException 异常,除非您打算重新抛出原始异常。

所以当你抛出异常时使用Exception

【讨论】:

  • 哦,抱歉,是的。谢谢!我会接受的。我只是新手,所以我真的不知道会发生什么。
  • 计划在.Net Standard 2.0+中支持github.com/dotnet/corefx/issues/4222
猜你喜欢
  • 2017-10-24
  • 2011-03-26
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-09
  • 1970-01-01
相关资源
最近更新 更多