using System; using System.Windows.Forms; public interface IExceptionHandler { bool Handle(Exception ex); } public class MessageBoxExceptionHandler : IExceptionHandler { protected string msg, tit; public bool Handle(Exception ex) { if (msg == null) return false; MessageBox.Show(tit, msg, MessageBoxButtons.OK, MessageBoxDefaultButton.Button1); return true; } } public class FileExceptionHandler : MessageBoxExceptionHandler, IExceptionHandler { string filename; const string msgInvalido = "O arquivo {0} é inválido!"; const string msgSemPermissao = "Sem permissão para ler o arquivo {0}!"; public FileExceptionHandler(string filename) { this.filename = filename; } public bool Handle(Exception ex) { tit = String.Format("Erro na leitura do '{0}'", filename); msg = null; if (ex is System.IO.IOException) msg = msgInvalido; else if (ex is System.UnauthorizedAccessException) msg = msgSemPermissao; else if (ex is System.InvalidOperationException) msg = msgInvalido; if (msg != null) msg = String.Format(msg, filename); return base.Handle(ex); } } public class Exemplo { public static void Main(string[] args) { } void DeserializeProfiles() { try{ XmlSerializer serializer = new XmlSerializer(typeof(ProfilesCollection)); Stream file = new FileStream("profiles.xml",FileMode.Open); XmlReader reader = new XmlTextReader(file); Profiles = (ProfilesCollection) serializer.Deserialize(reader); reader.Close(); } catch (Exception ex){ if (!(new FileExceptionHandler("profiles.xml").Handle(ex))) throw; } } }