What type of exception is in this case?

I am writing a C # application that uses automation to control another program. Naturally, the program must work for my program to work. When my program is looking for an application and cannot find it, I would like to throw an exception (now, of course, I could try to open the application or tell the user to open it or ...).

Should I implement my own exception - or use an existing NotSupportedException (or one of the other .NET exceptions). If a custom exception, what would you suggest? I was thinking of implementing a custom exception, which I would call MyAppNameException, and then just use the message to declare what is the problem?

Are there any general rules for throwing exceptions in such a way that your program is more readable and user friendly, or am I just thinking too much about it :)?

Thanks!

+7
c # custom-exceptions
source share
4 answers
  • First, define MyAppCustomException as an abstract base class.

  • Then we inherit it using AppNotFoundCustomException .

Thus, you can catch all exceptions from your application or only specific ones.

Here is an 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 is an example try/catch client:

 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 a problem with our app } 
+8
source share

The Framework Rule Reference that I use indicates that you should only create a custom exception if the error condition can be programmed in another than any existing exceptions.

In your case, if you want to create a custom exception to run the built-in installer, this is unique, and I think the custom exception will be fine.

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

+3
source share

Yes, you overdid it. Nothing good will happen when you throw an exception, any exception that the program will not magically start working when you do this. Only bad things can happen, for example, some code actually catches this exception and tries to continue. Or no one caught it and received the Windows Error Reporting dialog box. It is also possible to put a message box and call it day using Environment.Exit ().

Of course, this can be more useful for the user if you really run this program if you find it is not running.

+1
source share

You, of course, should not use NotSupportedException because your application supports this method. NotSupportedException is used when implementing an interface or abstract class, but some of them are not fully implemented, since they do not make sense in the context (reading from the output stream, clearing the read-only collection, etc.).

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

You say an โ€œapplicationโ€ that offers an executable file, not a component for use by others. In this case, you will not throw an exception before the calling code (because the code does not call), but either raise the dialog (for the GUI application) or write to Console.Error (for the console application). This makes it likely that either you are just going to display the value of the Message property for an exception, or you just need a class type to mark a specific message. Either just throwing an AppNotRunningException from an Exception, or just using Exception directly, will probably work fine, depending on which of the two most convenient.

0
source share

All Articles