From 240a543de87fc97a854e3007c5e9b334d65e993a Mon Sep 17 00:00:00 2001 From: Antony Male Date: Wed, 23 Jul 2014 15:19:30 +0100 Subject: [PATCH] Remove all instances of 'throw new Exception' --- Stylet/Screen.cs | 2 +- Stylet/ViewManager.cs | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Stylet/Screen.cs b/Stylet/Screen.cs index 65d0cd2..4f117a9 100644 --- a/Stylet/Screen.cs +++ b/Stylet/Screen.cs @@ -158,7 +158,7 @@ namespace Stylet void IViewAware.AttachView(UIElement view) { if (this.View != null) - throw new Exception(String.Format("Tried to attach View {0} to ViewModel {1}, but it already has a view attached", view.GetType().Name, this.GetType().Name)); + throw new InvalidOperationException(String.Format("Tried to attach View {0} to ViewModel {1}, but it already has a view attached", view.GetType().Name, this.GetType().Name)); this.View = view; diff --git a/Stylet/ViewManager.cs b/Stylet/ViewManager.cs index 202a51a..8b147b7 100644 --- a/Stylet/ViewManager.cs +++ b/Stylet/ViewManager.cs @@ -83,7 +83,7 @@ namespace Stylet // TODO: This might need some more thinking var viewType = AssemblySource.Assemblies.SelectMany(x => x.GetExportedTypes()).FirstOrDefault(x => x.FullName == viewName); if (viewType == null) - throw new Exception(String.Format("Unable to find a View with type {0}", viewName)); + throw new StyletViewLocationException(String.Format("Unable to find a View with type {0}", viewName), viewName); return viewType; } @@ -111,7 +111,7 @@ namespace Stylet var viewType = this.LocateViewForModel(model.GetType()); if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType)) - throw new Exception(String.Format("Found type for view: {0}, but it wasn't a class derived from UIElement", viewType.Name)); + throw new StyletViewLocationException(String.Format("Found type for view: {0}, but it wasn't a class derived from UIElement", viewType.Name), viewType.Name); var view = (UIElement)IoC.GetInstance(viewType, null); @@ -141,4 +141,26 @@ namespace Stylet viewModelAsViewAware.AttachView(view); } } + + /// + /// Exception raised while attempting to locate a View for a ViewModel + /// + public class StyletViewLocationException : Exception + { + /// + /// Name of the View in question + /// + public readonly string ViewTypeName; + + /// + /// Create a new StyletViewLocationException + /// + /// Message associated with the Exception + /// Name of the View this question was thrown for + public StyletViewLocationException(string message, string viewTypeName) + : base(message) + { + this.ViewTypeName = viewTypeName; + } + } }