简体   繁体   中英

How to throw the exception in this case - c#

Well, I have a class that its constructor checks whether a file exists. If the file does not exist I throw a new exception.

The problem is that when the exception is thrown, the user can see all my code ...

No way, the exception is detected from where the user instantiated class?

for example

the launch of the exception is happening here, this way, the programmer can see it all in class

if (!File.Exists(FileLocation))
        {
            throw new TFDException("File not found in the provided directory.");
        }

but would like to happen here,that's where I instantiate the class

TFDConnection con = new TFDConnection("D:\\File.tfd");

You can do this

try
{
    TFDConnection con = new TFDConnection("D:\\File.tfd");
}
catch(Exception exx)
{
}

to catch exception

If source code won't be available for other programmers at the same location, which is defined in pdb files, they won't see actual source code in exception. But in general if other programmer will really want to find what's going on, nothing will help even obfuscators, they only will make it little harder... But there is another thing - why you are throwing exception in constructor? It's not very good practice, because it could leave some resources in unkown state... Better is to avoid such things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM