Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C C# VB.NET
Hi, I have method 1 and Method 2 without try catch and Method 3 with try catch. consider following code.
 
Method1()
{
Method2()
' doing some stuff
}
 
Method2()
{
method3()
' doing some stuff
}
Method3()
{
try
catch ex as Exception
throw ex
end try 
}
 
if Exception occurred in Method 3 on throwing ex, code after from where method 3 is called in method2 ll not get executed also ll not get executed from method1.
 
Now what I need is to handle exception in Method2. consider following.
 
Method1( )
{
Method2( )
' doing some stuff
}
 
Method2( )
{
try
method3( )
' doing some stuff
catch ex as Exception
messagebox.show("")
end try
}
Method3( )
{
try
catch ex as Exception
throw ex
end try 
}  
Now in this case exception from method3 ll be handle in method2 but after handling that exception I Want to stop executing code in method1 from where method 2 is called. i dont want to change anything in Method 1, because its been called from thousand of other places. Smile | :)
i can I achieve this. Please help
Posted 5-Apr-13 1:36am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Put a try catch in method 1 and re-throw the error from method 2. would be the simplest way to do it.
  Permalink  
Comments
n3rdy at 5-Apr-13 6:53am
   
I cant Change Method1 as I mentioned before, I have to do something in catch of Method2 so that All code execution stops from after where Method 2 is called.
Pheonyx at 5-Apr-13 6:56am
   
I think you need to re-evaluate your program then.
n3rdy at 5-Apr-13 7:09am
   
reason why i cant change in method 1 because its been called from 4000 places and i cant change in all those forms
Pheonyx at 5-Apr-13 7:12am
   
why are you looking at changing it in all those places?
Method 1 should be defined in one location, and can be called in lots of locations.
Changing the code within method 1, should not result in 4000 changes.
 
If you update the method 1 code, so that it has a try/catch around where it calls method 2, then if an error occurs you can put "return;" in the catch part of it if you wish to do nothing with the exception and just exit method 1.
n3rdy at 5-Apr-13 7:34am
   
method 2 is common in all those,
whats happening here is method 2 returns dataset, and in method 1 they are doing some thing like this
if ds.table(0).rows.count > 0 {}
 
what was happening before that when method 3 throws exception than no effect happen here as this ll not get executed in method 1, now method 2 returns nothing and all these method 1 produces new exceptions as there are no checks like this
if ds isnot nothing andalso ds.Tables.count > 0 etc etc. :(
Pheonyx at 5-Apr-13 7:53am
   
If all your method 1's are checking that the dataset returned from method 2 has rows, then why not just return a dataset containing a table with no rows? then it shouldn't process the result.
If that doesn't work, then you are out of luck I think.
n3rdy at 5-Apr-13 8:54am
   
I am out of luck :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If I understand you correctly then what you need is to rethrow the exception in Method2 just as you do in Method3 within the catch clause.
messagebox.show("");
throw ex;
If you do this then the remaining code in Method1 will not get executed, it wil get bypassed in the search for an exception handler.
If you don't want your code to crash wih an unhandled exception fault you'll need to have an exception handler further down the stack in something that ultimately calls Method1 so that the exception will get caught somewhere.
 
Generally though this is not looking like an ideal solution. Complex flow of control like this has a major tendency to lead to memory leaks, especially when excepton handling is involved. If these 'exceptional' cases are in fact part of the design of how the system is intended to work then they aren't really exceptional and should be coded for in another way.
  Permalink  
Comments
n3rdy at 5-Apr-13 6:52am
   
This is what I tried But it ll kill purpose, because am showing custom error in Method2, after showing error if I throw ex again, then default system exception ll also get shown, i dont want to show default Exception by throwing ex again :(
Matthew Faithfull at 5-Apr-13 7:10am
   
The only way to avoid the default exception is, as I mentioned to put a catch in a function higher up (That's all the default exception is anyway). Perhaps install you own default exception handler in main that silently catches this rethrown exception from Method2 and doesn't rethrow it again. This is not recommended practice but it will solve your immediate problem.
n3rdy at 5-Apr-13 7:36am
   
thats the point, my main is actually method 1, which is actually hundreds of forms load events, that is calling method 2,
method 2 is common in all those, whats happening here is method 2 returns dataset, and in method 1 they are doing some thing like this if ds.table(0).rows.count > 0 {} what was happening before that when method 3 throws exception than no effect happen here as this ll not get executed in method 1, now method 2 returns nothing and all these method 1 produces new exceptions as there are no checks like this if ds isnot nothing andalso ds.Tables.count > 0 etc etc. :(
Matthew Faithfull at 5-Apr-13 7:40am
   
Then it looks like you are out of luck, you'll have to appeal to whoever owns Method1 to be able to change the design.

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 544
2 Maciej Los 325
3 DamithSL 223
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100