Handle the exceptions you know you can handle as close as possible.
If you have a place no exceptions can be allowed to escape from, catch the root exception type - this will also catch all derived exceptions. But do this catch last, after the exception types you have different handling for.
If all your exception handling does is log an error, you only need to capture the root type.
You should know the .NET framework well enough to know what exceptions a method throws. For example, opening a file may cause an exception if the file doesn't exist, etc. Why would you have an application level exception handler for that? The method that opens the file should pop up a message box saying the file was not found.
When creating big applications(even medium apps) there are so many different kinds of exceptions that can come?
How to go about handling them?
Ideally by the following
1. Anticipate expected conditions that can result in errors. These of course vary by the area of the application/enterprise in which they appear.
2. Consider what the application/enterprise should do when those occur. This includes how or even if it should be logged.
3. Implement the code based on 1/2
4. Unit test the code of 3, including the exception generation
5. Besides the above boundary layers should ALWAYS assume that unexpected errors will occur. Those exceptions should be caught and logged once (not every layer.) Each layer must determine what the result is when an unexpected exception occurs and what the layer (or specific parts of the layer) do when an unexpected exception occurs.
As an example...
A database layer should always expect that communication errors will occur. There are various ways of dealing with this (which do not matter for this discussion) but they should still be expected. Even a clustered database solution can experience communication failure. Whether this is logged depends on how it is handled but even if the database layer is returning it as a result failure is should not log a stack trace, but just log it as a single error (db comm error.)
A database layer should not normally expect a SQL syntax error. That is something that should normally only occur during development or QA. So if it occurs one must log it. The database layer can handle this as a failed result.
I have this small feeling that we have more control on our application when we manually code it instead of using a code generator. Is it true?
If so then how often should we use a code generator and to what extent?
If not so then can someone guide me to a good place where I can learn about writing code generators?!
And what one would do when decide to open designer of such a form? I would guess that they wouold come to you to punch you
I very often edit them but not remove them, so i guess I am not withbout sin. :p
They're not really generated code, they're code which is manually edited using a graphical IDE. That is, every line in a .designer.cs file is there because you set a property or clicked something in the graphical editor.
What about the performance issues?
Will the code generator not weigh down the application?
Oh and is there any difference a code generator and a template (for an application)? Also is there a relation between the two?
All occasions I've seen and used code generation tools, that's been a build-time issue, not run-time. You run the tool to create code, which is included in the compiling and build of the software. As such, there's no performance penalty when running, although it can complicat the build process.
For instance, Microsoft has a tool that takes an XML file and creates an XML schema from it. This schema can then be run through the same tool which can generate C# classes to represent it. This means you don't have to hand code the classes needed to serialise the XML, but have used a code generation tool. Typically, you'd only do this when there's been a change in the XML schema, not every time you build the system.
I'm not entirely clear what you mean by template here? Do you mean the different application and library types you can pick from when creating a new project in Visual C#? Those templates are effectivel recipes used by the code generation tool to create the project and its initial files.
I very much dislike generated code. If you need so much code to accomplish something that you feel the need to generate it, there is a more fundamental problem that needs addressing, perhaps in the language or choice of technology.
I don't include code which is generated from a non-text editor, like WinForms designer files or WPF/Silverlight XAML. You're still in charge of that, it's just the text editor isn't your usual editing environment. But the abominations that Microsoft create for you when you communicate with a COM or WCF server are fairly clear indications that they need to provide better tools to access those technologies.
But is it not true that developers at entry level have to use a much more hands-on approach, whereas developers at a higher level will have to concentrate on the design of the application from an architectural point of view.
Also as you reach a higher level your responsibilities will increase. Will you use up more of your time by manual coding when you can save your time with a good code generator?
When I first started out as a developer many years ago, I thought that a code generator was a time saver and a great idea. The more experienced I got, the more I realized that code generators end up costing more time than an experienced developer writing it right the first time. Troubleshooting code from a generator is very often far more time consuming.
I have mentored a few developers over the years and I always try to steer them away from using "the easy button" because you don't learn much in development without doing the work yourself. So all in all, I don't like generators at all and I've found that most seasoned developers have tended to come to the same opinion I have of them. Cheers.
What a load of rubbish, you should be using a code generator where it is a valid tool for the job. Coding up the DAL layer is just crazy, coding up the model is just as dumb. Generating code for VM or the VIEW is impossible.
Obviously we are using Silverlight but the code generator we use started life as a VB5 tool in the 90s, it is called ClassBuilder, I pinched it from the lead developer on that project and have rewritten it a dozen times. It does the CRUD stored procedures, the Model (based on a view of the table) and the DAL. Half a dozen click and I have my WCF written.
We tried writing something for the VM and the View but that is where the hand coding is required.
Never underestimate the power of human stupidity
suppose I have a class A, and the interfaces I1 and I2.
Interfaces I1 contains methods a,b
Interfaces I2 contains methods a,b,c and d
class A can implements I1 or I2 depending on how it is constructed.
Question, what is the best structure to organize it?
one solution is to create a base class for Class A which implements only I1, and make Class A derived implementing I2:
Class A: I2, BaseA