Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I forgot to write try catch block in my project. The project contain 1000 pages. How can we solve this.
Posted
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 0:45am    
How did you measure those "pages"? Do you write programs on paper? :-)
Why do you think it presents a problem for exception handling.
--SA
valza 18-Oct-11 1:54am    
B V Sreenath301 ----
I don't understand.
You can write 1000 pages in your project, but you can't handle exceptions!?
It doesn't make sense?

Writing many try-catch-finally blocks is a typical sign of a developer who does not understand how structural exception handling works. This thing is one of the greatest inventions if software development which allowed to avoid boring and error-prone "error handling". It also allows to separate "regular" code from handling of exceptional situation. The whole idea of the technology is to write software freely, completely ignoring possibilities of such situation. When some handling is needed, it can be easily added later. This is even better.

So, don't worry, nothing is lost.

You only need one try-catch-finally block on the very top of the stack of each thread, in some cases two nested blocks. Event-oriented UI is a special case: you need to have a block inside the main even processing loop of the UI thread. This is already implemented in UI libraries. For example, both WPF and System.Windows.Forms provides special events you need to handle.

On top of it, there are few special cases when an exception should be caught somewhere in between. Most typically, it should be re-thrown, either as is or as a different exception type (more specialized (semantic) or, more rarely, as more generalized exception). Blocking exception propagation is quite a bad thing, but sometime should be used, usually to recover from exception thrown by "bad" 3rd-party code which is not accessible for patching.

For more detail about these practices, please see my past solutions:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Error Logging and Screen Shot.[^],
Catching an Exception[^].

—SA
 
Share this answer
 
v4
Comments
Bala Selvanayagam 18-Oct-11 10:45am    
Well said 5ed
Sergey Alexandrovich Kryukov 18-Oct-11 11:22am    
Thank you, Bala.
--SA
Try and add try-catch exceptions for the public methods.
That should make it easier and faster. The private method errors would propagate to the public method handlers (unless you want a private method to launch a specific error).
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 0:53am    
Abhinav, please read my solution and think about it. Why adding exception to public methods, ever? All of it? I did not vote, but better fix or remove it. Honestly.
--SA
I am at loss at what to say about this.
IMHO, you got 3 options :
1. add throws Exception on every method you could found and add a try-catch in the main method.
2. add try-catch in every method you could found.

Finally, If you're paid:
3. Re-check the code, page by page, add try-catch as you see necessary. (might take days or weeks, but it's the best option)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 0:47am    
WARNING!!! Never ever use these "advices", anyone -- it's wrong an can create a lot of problem and even destroy the project! My vote of 1.
--SA
Firo Atrum Ventus 18-Oct-11 1:10am    
@SA:
I understand that the first and second "advices" are extremely bad practices that should never be done. However I have my own reasons for posting this :
1. "forgot to write try catch block" = Ultra bad premonition.
2. "1000 pages" can mean two things a Web or 1000 class.
3. The fact that he choose to ask here, instead of start working on it. It means, he want a shortcut for his problem.

Therefore, my third "advice" is my way of saying "No, there is no shortcut, get back on your code and fix it, step by step."

[edit]
After looking at my reply, my first thought is, "I'm a jerk" (which is not my intention). So please don't misunderstand me, I completely agree with you about this matter, it's just that I met these kind of programmer all the time and the best method to lead them to the correct path is the *hard* way.
-FAV
[/edit]
check the code, page by page, add try-catch as you see necessary. This is the best option only.Because there is no any throws clause in the c #.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 0:50am    
True, there is not "throw clause". There is a throw statement. My vote of 1.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900