C# Error Handling Events
Contents |
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft c# error handling in constructor Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs
C# Error Handling Get Line Number
Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The c# error handling framework content you requested has been removed. You’ll be auto redirected in 1 second. .NET Framework Class Library System.IO Namespaces System.IO System.IO ErrorEventHandler Delegate
C# Error Handling Techniques
ErrorEventHandler Delegate ErrorEventHandler Delegate BinaryReader Class BinaryWriter Class BufferedStream Class Directory Class DirectoryInfo Class DirectoryNotFoundException Class DriveInfo Class DriveNotFoundException Class DriveType Enumeration EndOfStreamException Class ErrorEventArgs Class ErrorEventHandler Delegate File Class FileAccess Enumeration FileAttributes Enumeration FileFormatException Class FileInfo Class FileLoadException Class FileMode Enumeration FileNotFoundException Class FileOptions Enumeration c# error handling class FileShare Enumeration FileStream Class FileSystemEventArgs Class FileSystemEventHandler Delegate FileSystemInfo Class FileSystemWatcher Class HandleInheritability Enumeration InternalBufferOverflowException Class InvalidDataException Class IODescriptionAttribute Class IOException Class MemoryStream Class NotifyFilters Enumeration Path Class PathTooLongException Class PipeException Class RenamedEventArgs Class RenamedEventHandler Delegate SearchOption Enumeration SeekOrigin Enumeration Stream Class StreamReader Class StreamWriter Class StringReader Class StringWriter Class TextReader Class TextWriter Class UnmanagedMemoryAccessor Class UnmanagedMemoryStream Class WaitForChangedResult Structure WatcherChangeTypes Enumeration TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. ErrorEventHandler Delegate .NET Framework (current version) Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 .NET Framework 3.5 .NET Framework 3.0 .NET Framework 2.0 .NET Framework 1.1 Represents the method that will handle the Error event of a FileSystemWatcher object.Namespace: System.IOAssembly: System (in Sy
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies
Error Handling In C# Best Practices
of this site About Us Learn more about Stack Overflow the company
Error Handling C# Mvc
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges multicast delegate exception handling c# Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: https://msdn.microsoft.com/en-us/library/system.io.erroreventhandler(v=vs.110).aspx Sign up Using events rather than exceptions to implement error handling up vote 10 down vote favorite 4 I'm working on some code that uses a pattern in its business and data tiers that uses events to signal errors e.g. resource = AllocateLotsOfMemory(); if (SomeCondition()) { OnOddError(new OddErrorEventArgs(resource.StatusProperty)); resource.FreeLotsOfMemory(); return; } This looked superficially rather odd, especially as the code that calls http://stackoverflow.com/questions/148882/using-events-rather-than-exceptions-to-implement-error-handling this needs to hook into the events (there are four or five different ones!). The developer tells me that this way they can refer to the properties of the allocated resource in the error handling code, and that responsibility for cleaning up after the error is kept by this tier. Which makes some kind of sense. The alternative might be something like resource = AllocateLotsOfMemory(); if (SomeCondition()) { BigObject temporary = resource.StatusProperty; resource.FreeLotsOfMemory(); throw new OddException(temporary); } My questions are: As this "BigObject" is freed when the exception object is released, do we need this pattern? Has anyone else experience of this pattern? If so, what pitfalls did you find? What advantages are there? Thanks! c# design-patterns exception events share|improve this question asked Sep 29 '08 at 14:22 Jeremy McGee 16.7k64186 add a comment| 9 Answers 9 active oldest votes up vote 3 down vote accepted Take a look at this post by Udi Dahan. Its an elegant approach for dispatching domain events. The previous poster is correct in saying that you should not be using an event mechanism to recover from fatal errors
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/2328815/should-c-sharp-event-handlers-be-exception-safe or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it http://programmers.stackexchange.com/questions/96383/is-onerror-handler-better-than-exceptions only takes a minute: Sign up Should C# event handlers be exception safe? up vote 17 down vote favorite 5 Assuming that one event has multiple handlers, if any of event handlers throw an exception then the remaining handlers are error handling not executed. Does this mean that event handlers should never throw? c# .net share|improve this question edited Dec 26 '12 at 23:13 think123 7,8381153117 asked Feb 24 '10 at 19:24 Marko 2,80521635 add a comment| 6 Answers 6 active oldest votes up vote 9 down vote accepted Since invoking an event means that the caller has no knowledge about the callee: Invoking an event handler should be robust in the face of arbitrary exceptions. Everything up the call stack needs c# error handling to clean up its own mess correctly, in case something completely unexpected occurs. Event handlers should really avoid throwing exceptions. Things like null reference exceptions are really inexcusable in any code, so obviously we aren't concerned about that. Things like file IO exceptions can always happen when writing or reading a file, so I would avoid ever doing IO within an event handler. If it makes sense to do IO in an event handler, then it also makes senst to handle the IO exceptions within the handler too. Don't propogate that back to the caller. Find some way to deal with it. share|improve this answer answered Feb 24 '10 at 20:09 Jeffrey L Whitledge 37.8k54280 add a comment| up vote 7 down vote In an ideal world, yes. It's a good idea to try to design event handlers so that they: Don't throw exceptions Execute very quickly Not doing so will result in unexpected side effects, since other subscribers to the event may never receive messages, or receive them very late. share|improve this answer answered Feb 24 '10 at 19:26 Reed Copsey 394k377871110 add a comment| up vote 4 down vote You should NOT swallow exceptions in an event handler, but neither would I generally recommend throwing them. The problem is that there's generally no good place to catch exceptions thrown from within event handlers. The design pattern of event handlers leads to some potential problems because they a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Is onError handler better than exceptions? up vote 6 down vote favorite I understand that exception in program means that something unpredictable happened (but not so bad to unavoidably crash the application!). The try-catch-finally sequence makes me sad because the program is harder to read (one more level of curly brackets) and harder to understand (jump from anywhere to catch in case of exception happened, it is deprecated GOTO). Since we have OOP, I suggest to create proxy class which in case of exception consumes it silently, returns some default value and fires onError event. So if we propagate exception, we have onError call instead, and the two disadvantages mentioned above are solved. See the example in C#: Standard exception attitude class Computer { // let's say we need to propagate exception and decide what to do lately public int divide(int a, int b) { int result = a / b; return result; } } class Program { public static void Main() { Computer c = new Computer(); try { Console.WriteLine(c.divide(1, 0)); } catch(ArithmeticException e) { // we are teleported here from the middle of Computer.divide method! // do something } } } // three levels of brackets (without namespace) in such trivial example?? onError handler attitude class ProxyComputer { private Computer c = new Computer(); // it is not virtual, can not be overrid