What type of exception to throw in this case?

风流意气都作罢 提交于 2019-12-05 06:16:39
PostMan
  1. First, define MyAppCustomException as an abstract base class.

  2. Then inherit from it with AppNotFoundCustomException.

This way you can catch all exceptions from your app, or just specific ones.

Here's some example code that illustrates the concept:

public abstract class MyAppCustomException : System.Exception
{
    internal MyAppCustomException(string message)
        : base(message)
    {
    }

    internal MyAppCustomException(string message, System.Exception innerException)
        : base(message,innerException)
    {            
    }
}

public class AppNotFoundCustomException : MyAppCustomException
{
    public AppNotFoundCustomException(): base("Could not find app")
    {
    }
}

And here's a client try/catch example:

try 
{
   // Do Stuff
}
catch(AppNotFoundCustomException)
{
   // We know how to handle this
}
catch(MyAppCustomException) // base class
{
   // we don't know how to handle this, but we know it's a problem with our app
}

The Framework Guidelines book that I use indicates that you should only create a custom exception when the error condition can be programmatically handled in a different way than any existing exceptions.

In your case, if you wanted to create a custom exception in order to launch a back-end installation program, that is unique and I think a custom exception would be okay.

Otherwise, something from the System.Runtime.InteropServices.ExternalException heirarchy may be appropriate.

Yeah, you're overdoing it. Nothing good is going to happen when you throw an exception, any exception, that program isn't magically going to start running when you do. Only bad things might happen, like some code actually catching that exception and trying to continue. Or nobody catching it and getting a Windows Error Report dialog. Might as well put up a message box and call it a day with Environment.Exit().

Of course, it could be more useful to the user if you actually start that program if you find it isn't running.

You certainly shouldn't use NotSupportedException, as you suggest, because your application does support the method in question. NotSupportedException is used when a interface or abstract class is implemented, but with some members not fully implemented as they don't make sense in the context (reading from an output stream, clearing a readonly collection, etc).

A closer match is something InvalidOperationException, where a member can be used, but not given the current state.

You say "application", which suggests an executable rather than a component for use by something else. In this case you aren't going to bubble the exception up to the calling code (since there isn't calling code) but either raise a dialog (for a GUI app) or write to Console.Error (for a console app). This makes it likely that either you're just going to display the value of the Message property of the exception, or that you just need the class type to flag a particular message. Either simply deriving AppNotRunningException from Exception or just using Exception directly will probably serve perfectly well, depending on which of the two you find most convenient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!