Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. :)
i can I achieve this. Please help
Posted
v2

Put a try catch in method 1 and re-throw the error from method 2. would be the simplest way to do it.
 
Share this answer
 
Comments
Waqas Ahmad Abbasi 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 5-Apr-13 6:56am    
I think you need to re-evaluate your program then.
Waqas Ahmad Abbasi 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 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.
Waqas Ahmad Abbasi 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. :(
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.
 
Share this answer
 
Comments
Waqas Ahmad Abbasi 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 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.
Waqas Ahmad Abbasi 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 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)



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