Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
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,

C#
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,


C#
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 2: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.
Toniyo Jackson 13-Apr-11 8:29am    
He is a new member. First time you can give advice. This is too much.
Tarun.K.S 13-Apr-11 8:24am    
Use <pre> tags to wrap your code.

1 solution

This

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


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


and to this:

C#
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:

C#
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:
C#
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
 
Share this answer
 
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??
Sergey Alexandrovich Kryukov 13-Apr-11 8:19am    
Updated.
Please explain this Question. What line do you mean? (Show by "Improve question".)
--SA
Sergey Alexandrovich Kryukov 13-Apr-11 8:20am    
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
Sergey Alexandrovich Kryukov 13-Apr-11 8:57am    
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

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