Tag: exception

try-catch中未处理的exception

try { list = from XElement e in d.Descendants(wix + “File”) where e.Attribute(“Name”).Value.Contains(temp.Name) && e.Parent.Parent.Attribute(“Name”).Value.Contains(temp.Directory.Name) select e; } catch (NullReferenceException e) { MessageBox.Show(e.Message); } catch (Exception e) { MessageBox.Show(e.Message); } 现在我的问题是为什么这段代码产生运行时错误,说我有一个未处理的NullReferenceException。 如果您需要有关该计划的更多信息,请通知我。 编辑:调试器指向linq语句的“where”部分。 当我直接从exe文件运行这个程序时,我仍然得到exception。

处理未处理的exception后恢复程序执行

问题:我通常使用AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException捕获未处理的exception。 现在的问题是,使用此exception处理程序 Public Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Console.WriteLine(e.ExceptionObject.ToString()) Console.WriteLine(“Press Enter to continue”) Console.ReadLine() ‘Environment.Exit(1) End Sub 如果我不退出程序,程序执行仍会抛出exception。 现在我想知道如何将其更改为“on error resume next”行为? 有可能吗? _ Public Sub Main(ByVal argv As String()) ‘For i As Integer = 0 To argv.Length – 1 Step 1 ‘Console.WriteLine(“Argument {0}: {1}”, i, argv(i)) ‘Next […]

如何捕捉exception?

我试图调用api并检查它的响应,但是当传递了一些错误的值时它会停止程序。 我想在请求和响应期间添加exception,但不确定如何写入函数。 这就是我调用我的REST调用的方式 public dynamic APICalls(JObject ljson, string endpoints, string method) { var httpReq = (HttpWebRequest)HttprequestObject(endpoints, method); using (var streamWriter = new StreamWriter(httpReq.GetRequestStream())) { streamWriter.Write(ljson); streamWriter.Flush(); streamWriter.Close(); } var httpResponse = (HttpWebResponse)httpReq.GetResponse(); var result = “”; using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); } return result; //return “Success”; //not sure what to […]

EmguCv TypeInitializationException由EmguCv.CV.CvInvoke引发

让我首先说我确实已经遵循了很多教程,例如整个位于EmguCv主站点上的教程,但是抛出了一个TypeInitializationException。 现在,仔细聆听,因为这里出现了非常奇怪的部分。 我首先要说的是我的问题有三个“级别”,然而,所有“级别”中的代码完全相同,即使没有任何变化。 这自然会指出我有一个引用或链接问题,但我再次尝试了不同的教程,但无济于事。 级别1(此级别产生TypeInitializationException) 我创建一个新项目,正确引用所有内容等,然后在这个新项目中编写我的代码。 在调试时,我抛出了exception,我的程序退出。 这是一个问题图片的链接: http : //prntscr.com/uychc 等级2(此等级完全正常,不会抛出任何exception) 在这个级别中,我几乎找到了EmguCv的一个示例项目(在本例中为VideoSurveilance),然后删除默认代码并将我的所有代码复制并粘贴到那里。 添加了我需要的更多参考文献后,程序运行正常。 我不能发布超过3个链接,但你必须相信我,video图片显示正确。 等级3(这个级别不会引发exception,但警告我一个人的“第一次机会”) 在这个级别中,我将整个Level 2项目复制并粘贴到另一个目录中。 找到并重新链接丢失的文件/引用后,我能够运行程序,但图片没有显示,我得到一个“类型的第一次机会exception”System.TypeInitializationException“发生在Emgu.CV.dll警告.http:/ /prntscr.com/uycmn 我目前运行Windows 7 x64(是的,我将构建选项更改为x64和x64 .dll)并运行EmguCv 2.4.9和2.4.2(在两者上测试)和Visual Studios 2010和2012(在两者上测试)。 这是它可能值得的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; //using System.Threading.Tasks; using System.Windows.Forms; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using […]

获取/设置注册表ACL“SeSecurityPrivilege”时出现C#随机exception

我已经得到一个完全随机的exception,我可以运行1000次相同的代码集(每个“运行”是程序的完整结束,因此从命令行开始作为自己的进程然后存在)并得到它失败一次,甚至150次。 我的意思是我可以一遍又一遍地回溯它,它会完全随机失败。 System.Security.AccessControl.PrivilegeNotHeldException: The process does not possess the ‘SeSecurityPrivilege’ privilege which is required for this operation. at System.Security.AccessControl.Win32.GetSecurityInfo(ResourceType resourceType, String name, SafeHandle handle, AccessControlSections accessControlSections, RawSecurityDescriptor& resultSd) at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext) at System.Security.AccessControl.RegistrySecurity..ctor(SafeRegistryHandle hKey, String name, AccessControlSections includeSections) at Microsoft.Win32.RegistryKey.GetAccessControl(AccessControlSections includeSections) […]

打开文件时处理错误的最佳方法

处理文件(打开)是一种特别容易出错的活动。 如果您要编写一个函数来执行此操作(尽管是微不足道的),那么在处理错误的wrt中编写它的最佳方法是什么? 以下是好的吗? if (File.Exists(path)) { using (Streamwriter ….) { // write code } } else // throw error if exceptional else report to user 上述(虽然不是语法正确)是一个很好的方法吗?

C# – try {} catch(Exception ex){} – 不捕获任何exception

简短版本,此方法: public override async void MethodWithException() { throw new Exception(“any EXCEPTION type and format will be skipped by outer try-catch block”); } 没有被这个块捕获(跳过“catch”): try { realClassFromAbstractObject.MethodWithException(); Console.WriteLine(“Output in the console – NOT POSSIBLE but true!”); } catch (Exception exception) { //Nothing caught! Console.WriteLine(“2. Nothing in console, skipped exception! ” + exception); //— Notihng in the […]

System.TypeLoadException:无法从程序集“y”加载类型“x”

我试图将程序从VB6移植到C#,读取二进制文件并解析它。 我没有编译时错误或警告但是当我尝试运行它时,在它进入Main()之前它会抛出exception System.TypeLoadException was unhandled Message=Could not load type ‘Conversion.DataStructures.ClientOld’ from assembly ‘SandboxConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ because it contains an object field at offset 1 that is incorrectly aligned or overlapped by a non-object field. Source=SandboxConsole TypeName=Conversion.DataStructures.ClientOld StackTrace: at sandbox.Program.Main(String[] args) at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() […]

“双向”和XmlParseValidationException中的ExceptionValidationRule

我有一个数据库中的数据的WPF应用程序。 我可以在我的Appli中创建新数据并保存到数据库,我可以从我的DataBase加载数据。 例如,我创建了一个Person类。 在我的WPF应用程序中,我可以创建一个Person和ave到DataBase。 当我创建一个客户端时,一些文本框是带有ExceptionValidationRule的,并且,如果validation为false,它会出现一个带有详细错误的标签,一切正常:) 我的问题非常简单:在我的App Lauching中,在我的MainWindow()中,我在DataBase中获取所有Person对象并在DataGrid中加载它。 当我加载“Person对象”时,调用validation(因为validation过程是set()函数),如果有错误, throw new ApplicationException(….) 不是扔! 我有exception错误(XmlParseValidationException)消息,而不是说出错误的标签。 只有当数据加载到Person对象中时才会这样。 所以,得出结论: 如果我在文本框中输入绑定到Person对象的错误数据,如果validation为false,一切正常,我看到错误标签:) 如果我将数据库中的错误数据加载到Person对象,如果validation为false,则我的屏幕上会出现exception消息框! 这是我的XAML: 这是我的Person Class(为了更好的视图而清理): private string m_strCodePerson; public string strCodePerson { get { return m_strCodePerson; } set { m_strCodePerson = value; if (m_strCodePerson.Trim() != “”) { if (m_objIntervenant.ReadIntervenantCodebyCode(m_strCodePerson) != 0) { throw new ApplicationException(m_strCodePerson.Trim() + ” don’t exist !”); […]

仅当数组为空时,JSON反序列化才会失败

在某些情况下,当我收到其中一个数组属性为空的JSON时,反序列化失败,抛出以下exception: 无法将当前JSON对象(例如{“name”:“value”})反序列化为类型’SonicApi.ClickMark []’,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。 路径auftakt_result.click_marks,第1行,第121位。 尝试使用以下代码忽略空值没有帮助: var jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore; 以下是产生错误的JSON示例: { “status”: { “code”: 200 }, “auftakt_result”: { “clicks_per_bar”: 0, “overall_tempo”: 0, “overall_tempo_straight”: 0, “click_marks”: {} } } 以下是JSON的示例,其数组不为空且不会产生任何错误: { “status”: { “code”: 200 }, “auftakt_result”: { “clicks_per_bar”: 8, “overall_tempo”: 144.886978, “overall_tempo_straight”: 144.90889, “click_marks”: [ { “index”: 0, “bpm”: […]