-2

I created my own class library and I have create() method like this:

public  int create()
{
    try
    {
        if(path!=null){
        //do somethings
        }
        else{
        throw new ArgumentNullException("path cannot be null ro empty", "path");
        }
        catch{
        throw;
        }
        return 0;
    }
}

In another project, I add my class library DLL and when I use my library method and because of something for example path=null my method thrown an exception and I get that in line that I wrote throw inside the catch... I don't want that,how can I get error in line that I call create() method in my project

Thank you

SORRY I DONT KNOW ENGLISH VERY WELL so i try again to say my mean I try to create class library and I want to get it to some one else to use, I want when exception in my create() method thrown visual studio highlight the line that create method was called, but it open my dll and go to create method and highlight the line that I wrote throw;... how can I solve it?

....................................................................................

finally I found the answer,see this link: Hiding code from a DLL while debugging

Community
  • 1
  • 1
siamak
  • 1
  • 3
  • 4
    I have no idea what you are asking. Please reformat and rephrase your question as of this moment it is impossible for me understand what you want. – RononDex Feb 24 '14 at 09:46
  • Ah I think I understand what you mean, simply remove the try catch block – RononDex Feb 24 '14 at 09:48
  • 1
    @siamak I think you want to catch the exception from where you are calling the function ?? – Dot_NET Pro Feb 24 '14 at 09:48
  • sorry i dont know english very well,i try to create class library and i want to get it to some one else to use,i want when exception in my create() method thrown visual studio highlight the line that create method was called,but it open my dll and go to create method and highlight the line that i wrote `throw;`... how can i solve it – siamak Feb 24 '14 at 10:09
  • finally i found the answer,see this link: [Hiding code from a DLL while debugging][1] [1]: http://stackoverflow.com/questions/17484053/hiding-code-from-a-dll-while-debugging – siamak Feb 25 '14 at 07:19

4 Answers4

0

To stop the debugger at the correct line when you throw your exception, all you have to do is to remove your try catch block (which is completely unnecessary in your case either way):

public  int create()
{      
    if(path==null)
        throw new ArgumentNullException("path", "path cannot be null or empty");

    // do something

    return 0;
}

Also as Oscar pointed out, you should switch the arguments when throwing the exception, as the first parameter is for the parameter name, the second for the message.

RononDex
  • 4,143
  • 22
  • 39
  • i remove the `try catch` block but still when exception in my create() method thrown visual studio open my dll and go to create method and highlight the line that i wrote throw; i want that it highlight the line that user call the create method – siamak Feb 24 '14 at 10:28
  • @siamak Then you have to build your dll with "Release" (no debugging information), then reference that dll in your project. The user will only see that line if he has the source code of that dll on his machine, which is normally not the case – RononDex Feb 24 '14 at 11:54
0

You have swapped the arguments of ArgumentNullException constructor. First goes the param name, and later the message.

public ArgumentNullException(
    string paramName,
    string message
)

http://msdn.microsoft.com/es-es/library/k8a0dfcy(v=vs.110).aspx

Also, you can safely remove the catch clause, as you're doing nothing there. Only catch exception you suppose to handle somehow, otherwise, let it bubble up.

Oscar
  • 13,594
  • 8
  • 47
  • 75
0

When you rethrow an exception, only one stack frame is saved inside each method, therefore, you can't find out which line threw the exception, only the line that rethrew it. You can either log the message when you catch it for the first time, or don't rethrow but instead throw a new exception, and supply the caught exception as an inner exception.

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
-1

You can always do check before try :

public  int create(){
 if(path!=null){
     try{

     //do somethings
      }
     catch{
      throw;
  }
  else{
      throw new ArgumentNullException("path cannot be null ro empty", "path");
  }
  return 0;
}
Radenko Zec
  • 7,659
  • 6
  • 35
  • 39