Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the code here where i manually throw arguementnullexception. I need to throw to the user but when i run the code it goes to the throw statement at the catch part rather at the place where the user calls it even after re-throwing.

What I have tried:

        public Services(string hostName) 
        {
            try
            {
               
                if (hostName!=null)
                {
    
                }
                else
                {
                    ManuallyThrowException(hostName); //if not try catch it displays here
                }
            }
            catch(Exception e)
            {
                throw; //but it is displaying here if try catch
            }
        }

public static void Main()
{
        Services(null); // i need to display exeception here
}
Posted
Updated 7-Apr-20 22:15pm
v2
Comments
Tomas Takac 8-Apr-20 2:23am    
It is not clear what you are asking. The code is supposed to go to the catch in your method, then the same exception is re-thrown and caught somewhere up the call chain. Or crashes your program if not caught. The try-catch as shown in your example is useless, you may as well remove it. Anyway, I think you should update your question and also show the code which calls this method but you don't get the exception there as expected.
Member 14580921 8-Apr-20 3:25am    
I have modified and added comments for clarity.Please help me how to achieve it

1 solution

You need to catch the exception in your main method:
C#
try
{
    Services(null); // i need to display exeception here
}
catch (Exception me)
{
    Console.WriteLine(me.Message);
}
 
Share this answer
 

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