Tag: error handling

使用ToArray()将列表转换为数组

我创建了一个名为listItem的类和以下列表: List myList = new List(); 在我的代码中的某个时刻,我想将其转换为数组,从而使用: listItem[] myArray = myList.ToArray(); 不幸的是,这不起作用,我收到此错误消息: Cannot convert […] listItem[] to […] List 我试图解决这个问题,但非常失败…… 提前致谢。 编辑:我的坏,我写的第一个代码行确实是错误的! 实际上,上面的所有代码都运行良好。 我的错误是由于我的function: List myFunction() 返回myArray,因此转换问题……现在已经修复了。 🙂 谢谢大家的答案。

C#的最佳选择“On Error Resume Next”是什么?

如果我为我的C#代码添加空catch块,它是否等同于VB.NET的“On Error Resume Next”语句。 try { C# code; } catch(exception) { } 我问这个的原因是因为我必须将VB.NET代码转换为C#,旧代码有~200“On Error Resume Next”语句,尽管我在新代码中使用了正确的try {} catch {} ,但有更好的选择吗?

System.NullReferenceException未将对象引用设置为对象的实例

所以,我明白了 System.NullReferenceException Object reference not set to an instance of an object. at Microsoft.Windows.Design.Platform.SilverlightMetadataContext.SilverlightXamlExtensionImplementations.d__8.MoveNext() at MS.Internal.Design.Metadata.ReflectionProjectNode.BuildSubsumption() at MS.Internal.Design.Metadata.ReflectionProjectNode.SubsumingNamespace(Identifier identifier) at MS.Internal.Design.Markup.XmlElement.BuildScope(PrefixScope parentScope, IParseContext context) at MS.Internal.Design.Markup.XmlElement.ConvertToXaml(XamlElement parent, PrefixScope parentScope, IParseContext context, IMarkupSourceProvider provider) at MS.Internal.Design.DocumentModel.DocumentTrees.Markup.XamlSourceDocument.FullParse(Boolean convertToXamlWithErrors) at MS.Internal.Design.DocumentModel.DocumentTrees.Markup.XamlSourceDocument.get_RootItem() at Microsoft.Windows.Design.DocumentModel.Trees.ModifiableDocumentTree.get_ModifiableRootItem() at Microsoft.Windows.Design.DocumentModel.MarkupDocumentManagerBase.get_LoadState() at MS.Internal.Host.PersistenceSubsystem.Load() at MS.Internal.Host.Designer.Load() at MS.Internal.Designer.VSDesigner.Load() at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedView.Load() at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedDesignerFactory.Load(IsolatedView view) at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory […]

通过等待任务或访问其Exception属性,未观察到任务的exception

这些是我的任务。 我应该如何修改它们以防止此错误。 我检查了其他类似的线程,但我正在使用等待并继续。 那怎么会发生这个错误呢? 通过等待任务或访问其Exception属性,未观察到任务的exception。 结果,终结器线程重新抛出了未观察到的exception。 var CrawlPage = Task.Factory.StartNew(() => { return crawlPage(srNewCrawledUrl, srNewCrawledPageId, srMainSiteId); }); var GetLinks = CrawlPage.ContinueWith(resultTask => { if (CrawlPage.Result == null) { return null; } else { return ReturnLinks(CrawlPage.Result, srNewCrawledUrl, srNewCrawledPageId, srMainSiteId); } }); var InsertMainLinks = GetLinks.ContinueWith(resultTask => { if (GetLinks.Result == null) { } else { instertLinksDatabase(srMainSiteURL, […]

命名空间’System’中不存在类型或命名空间名称’Linq’

我想在vs 2015中使用asp.net c#开发一个网站但是当我想使用按钮在c#中编写代码时,它不会转到cs文件来编写c#代码。当我检查项目源时,我发现了一个错误,它是 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; > Severity Code Description Project File Line Error CS0234 The type or > namespace name ‘Linq’ does not exist in the namespace ‘System’ (are > you missing an assembly > reference?) Golestani C:\Users\javad\Documents\Golestani\Login.aspx.cs 3 请帮我解决这个问题 图片

ASP.NET中的全局error handling

在全局ASP.NET应用程序中捕获错误是否有任何问题(例如:Global.asax)? 我已经看过一个好的error handling实践的问题,并没有真正说太多。 我的经验是排除了一些非常具体的情况(例如事务),我们编写的大多数ASP.NET应用程序都是如此 void ButtonEventHandler(object sender, EventArgs e) { Page.Validate(); if (Page.IsValid) { //Do a database insert or update thru relevant datalayers. //If its a transaction then we rollback internally and rethrow //the exception. } } 为什么不只有一个全局exception处理程序? 通常(此时)我唯一能做的就是优雅地中止操作并告诉用户再试一次。

捕获Global.asax中的错误

我的Global.aspx中有以下内容用于处理错误: void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); if (exception != null) { //Log if (HttpContext.Current.Server != null) { HttpContext.Current.Server.Transfer(“/siteerror.aspx”); } } } 这在很大程度上起作用,但有时不会进入Server.Transfer。 由于某种原因,HttpContext.Current.Server为null。 我想出了这种情况发生的地方:用户控件和业务逻辑类中发生错误。 我在这里错过了什么吗? 谢谢

如何从asp.net处理程序返回404错误?

我已经创建了一个下载文件的处理程序。 如果文件不存在或用户无权下载该特定文件,我想返回404错误。 可能吗? 如果有,怎么样? 示例代码将不胜感激。

try-catch块具有返回类型

如果我有一个返回某些东西的方法,比如 public DataTable ReturnSomething() { try { //logic here return ds.Tables[0]; } catch (Exception e) { ErrorString=e.Message; } } 这会产生编译器错误,显然是因为catch{}块不返回任何内容。 所以当我有返回值的方法时,我不使用try-catch块,这是一种不好的做法。 如果有错误,我想将错误字符串设置为该错误。 但是我还需要一个返回值。 建议吗?

Controller中的exception处理(ASP.NET MVC)

当您从控制器中的操作调用的自己的代码抛出exception时应该如何处理? 我看到很多最佳实践的例子,其中根本没有try-catch语句。 例如,从存储库访问数据: public ViewResult Index() { IList customModels = _customModelRepository.GetAll(); return View(customModels); } 显然,如果对无法访问的数据库进行调用,并且我们正在使用像Entity Framework这样的ORM,则此代码可能会抛出exception。 但是,我能看到的所有事情都会发生exception,并且会向用户显示一条令人讨厌的错误消息。 我知道HandleError属性,但据我所知,如果发生未处理的exception,它主要用于将您重定向到错误页面。 当然,这段代码可以包装在try-catch中,但不能很好地分离,特别是如果你有更多的逻辑: public ViewResult Index() { if (ValidationCheck()) { IList customModels = new List(); try { customModels = _customModelRepository.GetAll(); } catch (SqlException ex) { // Handle exception } if (CustomModelsAreValid(customModels)) // Do something else // Do something else […]