In this specific case, you should absolutely not be inheriting the Program
class that VS adds by default. That class should be the entry point for the application. If your application is very simple then you might put all the code in that one class. As your application gets more complex, you can move functionality out to other classes and use them in the Program
class. For such an application, you might have the initialisation code in the Program
class in one or more methods, then put the meat of the application functionality in other classes. Your Main
method would then call the initialisation method first and then create and use the other class(es) as required. In your case, you might have an ExceptionNotification
class with a Format
method. Anywhere in your application, you can then call ExceptionNotification.Format
. That's how it's supposed to work.
EDIT: Actually, I realise that I misinterpreted that method name. Maybe you would have a class named ExceptionNotifier
and a method named NotifyFormat
. You could then have other methods named NotifyX
, where X 是您要通知的异常类型的名称。
That said, I hope you're not really getting an exception thrown because the user didn't enter a number. If you're working with numeric user input then you should call TryParse
on the appropriate numeric type to validate and convert in one step. In that case, invalid input would not generate an exception so a type or member name that implies that it does would be inappropriate.