简体   繁体   English

在这种情况下如何引发异常-C#

[英]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. 如果源代码无法在pdb文件中定义的相同位置供其他程序员使用,则他们将不会看到实际的源代码异常。 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 这不是很好的做法,因为它可能会使某些资源处于未知状态...最好避免这种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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