Is catch(Exception) recommended to be used in .NET?

.NET has a very strong exception handling using try..catch blocks. Catch block is used to catch an exception and handle it as needed.



However usage of catch(Exception) is not recommended for various reasons. They are listed below:

• Catch(Exception) is a general way of catching exceptions. All the exceptions will be caught under this catch block and therefore it will be difficult to trap what the actual exception is. You should always use more specific exceptions like StackOverflowException, OutofMemoryException so that you will know more details about the problem.
• Using catch(Exception) has a considerable overhead. It is time consuming when compared to specific exceptions because Common Language Runtime (CLR) has to inspect every possible exception when you use the general catch statement catch(Exception).
• By using catch(Exception), you are trapping all errors in your code inside the catch block. Hence you are hiding errors from User and the errors will not be rectified. If you still want to use catch(Exception), throw meaningful errors to the User from inside catch block.
• Your application might use better exception handling in the next level after your code execution. If you use catch(Excpetion) and trap the error, the error will not get a chance to be handled in a better way by the next level of exception handler.
• By using catch(Exception), you make your application get into an unidentified state. This is not recommended.

| Can you call a constructor from another constructor of the Class in .NET? | Difference between Response.Output.Write() method and Response.Write() method in .NET | How do you establish multiple inheritance in C#? | How do you introduce a ReadOnly property in C#? | How do you perform constructor overloading in C#? | Is catch(Exception) recommended to be used in .NET? | What are the different access modifiers available in C#? | What are the different ways of overloading in C#? | What are the members of stringbuilder class in C#? | What is Multicast Delegate? Explain it with example in C# | What is the difference between abstract class and interface in .NET? | What is the difference between Clone and CopyTo methods in .NET | What is the difference between const and readonly in .NET | What is the difference between directcast and ctype in .NET? | What is the difference between out and ref parameters in .NET | What is the difference between public assembly and private assembly in .NET | What is the difference between strong typing and weak typing in .NET? | What is the difference between Trace and Debug in .NET | What is the need for Abstract Factory Pattern in C#? | What is the need for Adapter Pattern in C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.