Remove all instances of 'throw new Exception'

This commit is contained in:
Antony Male 2014-07-23 15:19:30 +01:00
parent b254f92fe8
commit 240a543de8
2 changed files with 25 additions and 3 deletions

View File

@ -158,7 +158,7 @@ namespace Stylet
void IViewAware.AttachView(UIElement view) void IViewAware.AttachView(UIElement view)
{ {
if (this.View != null) 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; this.View = view;

View File

@ -83,7 +83,7 @@ namespace Stylet
// TODO: This might need some more thinking // TODO: This might need some more thinking
var viewType = AssemblySource.Assemblies.SelectMany(x => x.GetExportedTypes()).FirstOrDefault(x => x.FullName == viewName); var viewType = AssemblySource.Assemblies.SelectMany(x => x.GetExportedTypes()).FirstOrDefault(x => x.FullName == viewName);
if (viewType == null) 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; return viewType;
} }
@ -111,7 +111,7 @@ namespace Stylet
var viewType = this.LocateViewForModel(model.GetType()); var viewType = this.LocateViewForModel(model.GetType());
if (viewType.IsInterface || viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType)) 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); var view = (UIElement)IoC.GetInstance(viewType, null);
@ -141,4 +141,26 @@ namespace Stylet
viewModelAsViewAware.AttachView(view); viewModelAsViewAware.AttachView(view);
} }
} }
/// <summary>
/// Exception raised while attempting to locate a View for a ViewModel
/// </summary>
public class StyletViewLocationException : Exception
{
/// <summary>
/// Name of the View in question
/// </summary>
public readonly string ViewTypeName;
/// <summary>
/// Create a new StyletViewLocationException
/// </summary>
/// <param name="message">Message associated with the Exception</param>
/// <param name="viewTypeName">Name of the View this question was thrown for</param>
public StyletViewLocationException(string message, string viewTypeName)
: base(message)
{
this.ViewTypeName = viewTypeName;
}
}
} }