Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace StructException
- {
- class Program
- {
- static void Main(string[] args)
- {
- Test t;
- try
- {
- t = new Test();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.ReadLine();
- }
- }
- class Test : SafetyFramework
- {
- // Something contains a thread or socket descriptor so must be cleaned up.
- FileStream fs;
- public Test()
- {
- Safe(() => fs = new FileStream("ErrorOut", FileMode.Open));
- }
- public override void Dispose()
- {
- Console.WriteLine("Disposing FS");
- if (fs != null) fs.Dispose();
- }
- };
- public class SafetyFramework : IDisposable
- {
- protected void Safe(Action a)
- {
- try
- {
- a();
- }
- catch
- {
- Dispose();
- throw;
- }
- }
- public virtual void Dispose()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement