Click here to Skip to main content
14,385,176 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am getting error as 'Exception was unhandelled by user code' on returning from content page's catch block
my code is as,

Public Void Vcf()
{
if (StateName1.Text == string.Empty || StateName1.Text == StringConstants.select ||
            StateName1.SelectedValue.ToString() == StringConstants.select)
{
    try
    {
        throw new  Exception(ErrorConstants.selectTheState);

    }
    catch (Exception exception)
    {
        throw exception;
    }
}
}


<---------------Here im gettting the exception error. From here my exception message should have to go to master page's catch block with exception message which is as,


private object CallContentFunction(string methodName, params object[] parameters)    
{
        try        
         {    Type contentType = this.Page.GetType();                
              System.Reflection.MethodInfo mi = contentType.GetMethod(methodName);                
              if (mi == null) 
                return null;                
              return mi.Invoke(this.Page, parameters);           
         }
        catch (Exception exception)  
        {           
          throw exception;        
        }    
}
Posted
Updated 13-Apr-11 3:23am
v3
Comments
Toli Cuturicu 13-Apr-11 8:12am
   
Can't you use pre tags?! Have my one then!
Tarun.K.S 13-Apr-11 8:24am
   
Whoa that was harsh.
Tarun.K.S 13-Apr-11 8:24am
   
Use <pre> tags to wrap your code.
Toniyo Jackson 13-Apr-11 8:29am
   
He is a new member. First time you can give advice. This is too much.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

This

try
{
    throw new  Exception(ErrorConstants.selectTheState);
}
catch (Exception exception)
{
    throw exception;
}


is equivalent to this:
try
{
    throw new  Exception(ErrorConstants.selectTheState);
}
catch {
    throw;
}


and to this:

throw new  Exception(ErrorConstants.selectTheState);


That's it, makes no sense. You did not catch the exception. Come to think about, you should not.

You probably mean to put under the try block this:

if (StateName1.Text == string.Empty || StateName1.Text == StringConstants.select ||
StateName1.SelectedValue.ToString() == StringConstants.select)


Here, StateName1.SelectedValue could be null, StateName1 should not be null (and most likely it is not). So, you should not assume something is selected, it throws exception at the attempt to call ToString.

Consider:
if (StateName1.Text == string.Empty ||
    StateName1.Text == StringConstants.select ||
    ((StateName1.SelectedValue != null) && (StateName1.SelectedValue.ToString() == StringConstants.select)))


Please see my directions on exception practices here:
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?[^].

—SA
   
v3
Comments
durgeshtupkar10 13-Apr-11 8:18am
   
i know that but what about the exception eroor which i m getting on leaving the try-catch of content page??
   
Updated.
Please explain this Question. What line do you mean? (Show by "Improve question".)
--SA
   
Probably I already answered, you did not see it yet when you were typing your follow-up question.
Is it clear now?
--SA
E.F. Nijboer 13-Apr-11 8:27am
   
Very correct. Don't catch exceptions you simply cannot handle.
Have a look at this article durgeshtupkar10, this article is very helpful: http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx
durgeshtupkar10 13-Apr-11 8:37am
   
consider i m getting condition as, StateName1.Text == StringConstants.select
then i go for throw new Exception with taking the exception message as"Select the City" to the catch block.From here also I come out and at the closing brace I get the error box with heading 'Exception was unhandelled byuser code' .
What should I do.
and one more thing ,I am new in c#
durgeshtupkar10 13-Apr-11 8:52am
   
but i have to use exception,it's necessary for next coding purpose.
   
This rule is one of the most apparent.
Those exceptions were invented (by Barbara Liskov et al) around 1974-75 for CLU and still too many "developers" has no really good idea about them.
--SA
   
Absolutely! You should use exceptions in all projects in every thread. So why are you not using them in this code? You're not using them but misusing; and this is opposite to using.
--SA
   
Fix the bug. I've shown you the code. Don't catch exception here.
Wait a minute...
--SA
   
Answer updated. Please read my last paragraph and use my two references.
--SA
durgeshtupkar10 13-Apr-11 9:12am
   
if i will not use exception how could i show the error msg on the maste page's label.
And plz tell why i m getting that exception error
   
You're not getting exception error, you're getting exception.
As I can see, you're getting it because selection returns null, and than you're trying to call ToString on the null instance, which through its.

You should not ask questions like that. You should go to Debugger, run into exception and provide exact exception dump (with stack), get source code line numbers and comment them in the code; post it.

--SA
durgeshtupkar10 13-Apr-11 10:23am
   
ok, sorry ,all agreed.
just suggest me how can i send the mesaage 'Select state' out of the Vcf() by using try-catch?
Sergey Alexandrovich Kryukov 13-Apr-11 10:58am
   
You do not save message here. Look, consider thoroughly: you need to catch exceptions only at the top of the stack of each thread, to prevent termination of application. In case of Forms it is only done in the main IO cycle of application, Application.OnThreadException, see my code samples. I have it in my reference, see. You only need to catch exception in between if you need some special processing to modify data in between. (User input error is one example.) So, in all you code you never should catch exception. The beauty of exception is: it is separated from code. Best way to handle exceptions: forget about it. Handling means blocking them. Handle only on top, 99% cases. You method does not need it.
--SA

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




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