The main intent of the new keyword in C# in this context is to let programmers and reviews know that this method is not an override and 'shadows' the base class method. This is specially targeted towards C++ programmers who migrated to C#. Remember, C++ does not have the override keyword.
Thew new keyword also enables you to 'shadow' or 'redefine' a virtual method in a derived class. IMO, this was not possible in C++. Correct me if I'm wrong.
A bigger difference is made in the line A ref2 = new B();
You are wrong, I suggest you try it yourself. As pointed out by Markovl and myself this post, removing "new" makes no difference besides silencing compiler warning.
Also, check out MSDN:[^]
When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.
This is just another useless facility only relevant in enterprise interviews.
Hi, is there any fool proof way to catch unhandled thread exception, whether it's a ThreadAbortException (caused by Thread.Abort from "Main" or UI thread) or a DivisionByZeroException (happenning within the thread function)?
I know the following won't catch it:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExHandler);<br />
And I don't think all developers will implement try-catch-finally on thread functions. How do you handle this?
I know for WPF app, in App.xaml you can define general handler for unhandled exceptions (But this will NOT catch Thread exceptions). For example, the following will NOT work:
Well, I allways have a try/catch in my threads. Usualy I catch TreadAbortException and other "most excpected" exceptions and at the end I allways catch general exceptions and log them.
In debug mode I tend to insert a Debug.Assert(false) in the general exception handler, to see if I forgot an "expected" exception.
In the release they are only logged.
Some people say, that you shold not catch general exceptions, but the software I'm used to work on has to run 24/7 and most of the time without a user in front of it. So it has te recover itself after the occurence of any kind of error.
I don't know how others manage it, but I don't like software which crashes totally, only showing a messagebox with an error and then say good-bye without any chance of saving data or anything else.
In WinForms, there is no way to prevent the termination of an app caused by an unhandled thread exception. AppDomain.UnhandledException will handle it, but will not prevent the app termination. The best you can do in this event handler is to save your data and gracefully exit the program.
I always have a top level try catch block in the method directly executed in a thread. This will handle all exceptions thrown from any where down in the call stack.
then it will end the program, and I would not have closed the connection to
database that is open.
When the program terminates the connection will be terminated as well.
And the only way you can terminate a database connection in C# anyways is if all of the following are true.
1. You have configured the connection pool to keep no idle connections.
2. There are no connections in use
3. You reset the pool.
Other than that just "closing" the connection does nothing more than return it to the connection pool.
- Are the two try/catch absolutely necessary? Try/catch is an expensive operation and I see many people using it where a simple if statement could save the day.
- Perhaps you can remove the second try, but move the catch to the first try. you can "assign" multiple catch handlers to one try. Of course only valid if you specify the exceptions. eg.:
//handle sql exceptions
//handle io exceptions
//handle all other exceptions
//handle anything that needs to be handled like freeing file handles, closing connections, ...//make sure you test for null values etc, because otherwise you'll get an error in the finally block, which is a bit silly.
- the quick and dirty way is maybe a goto statement, but you don't want to go that way, they're evil