Click here to Skip to main content
15,891,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a Way to test in a Method2 if there was a Exception in Method1?
(Called after each other: Method1 --> Method2)

Just to know if there was one?
Posted
Comments
shefeekcm 7-Jul-11 5:15am    
i did'n tget your question
Sergey Alexandrovich Kryukov 7-Jul-11 5:16am    
Does it mean Method1 calls Method2? Or just the opposite?
--SA

Testing anything for exceptions is a big abuse of the technology. Let exceptions go.

What to do? Please find some recommendations in my past answers:
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[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 7-Jul-11 14:15pm    
Good links, my 5 - personally I often log and rethrow the exception.
Sergey Alexandrovich Kryukov 7-Jul-11 14:47pm    
Thank you, Espen.
Re-throw? Sure, if you sometimes need to jump from raw exception to semantic exception of the custom type -- very robust approach.
--SA
Espen Harlinn 7-Jul-11 14:56pm    
I usually do this to enable a configurable debug mode using log4net - if a client experiences trouble, he/she will be able to provide me with valuable, accurate, information - having the user explaining what he is experiencing over the phone; often isn't accurate enough to determine what's going wrong.
Sergey Alexandrovich Kryukov 11-Jul-11 0:27am    
Very good.
--SA
In case you catch the exception in Method2 and after processing throw it back.
Something like this,
C#
sub method1()
{
 try
 {
  method2();
 }
 catch(exception ex)
 { 
  ...
 }
}
sub method2()
{
 try
 {
  ...
 }
 catch(exception ex)
 { 
  ...
  throw ex;
 }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jul-11 5:19am    
How about C#? It does not really test where the exception was. Also, method2 might not be called due to some code before it. Correct answer is: "testing" it makes no sense. Please see my solution.
--SA
You can always look at the stacktrace via code. For more information, see here[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jul-11 5:20am    
Of course, but why doing so? It won't "test" what OP wants. Method2 might no be called at all due to some prior exception.

Correct answer is: "testing" it makes no sense. Please see my solution.
--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