我一直在MSDN上阅读这篇关于Rfc2898DeriveBytes的文章.以下是它们提供的示例加密代码.
string pwd1 = passwordargs[0];
// Create a byte array to hold the random value.
byte[] salt1 = new byte[8];
using (RNGCryptoServiceProvider rngCsp = ne RNGCryptoServiceProvider())
{
// Fill the array with a random value.
rngCsp.GetBytes(salt1);
}
//data1 can be a string or contents of a file.
string data1 = "Some test data";
//The default iteration count is 1000 so the two methods use the same iteration count.
int myIterations = 1000;
try
{
Rfc2898DeriveBytes k1 = new …Run Code Online (Sandbox Code Playgroud) 我正在学习Python 3,并尝试编写一个将复制目录的脚本。我正在使用shutil.copytree。从Python文档中说:
如果发生异常,则会引发错误并列出原因。
此异常收集在多文件操作期间引发的异常。对于copytree(),exception参数是一个三元组的列表(srcname,dstname,exception)。
在示例中,他们这样做:
except Error as err:
errors.extend(err.args[0])
Run Code Online (Sandbox Code Playgroud)
这是我的脚本:
def copyDirectory(src, dest):
errors = []
try:
shutil.copytree(src, dest)
except Error as err:
errors.extend(err.args[0])
source="C:/Users/MrRobot/Desktop/Copy"
destination="C:/Users/MrRobot/Desktop/Destination"
copyDirectory(source, destination)
moveDirectory(destination,"I:/")
Run Code Online (Sandbox Code Playgroud)
问题:
您如何正确捕获使用时可能发生的异常shutil.copytree(假设以上脚本不正确)?
那您如何查看发生的错误,我将遍历整个errors阵列?
我正在创建一个删除子目录/文件的命令行实用程序.如果正在使用文件,System.IO.IOException则抛出该文件.我在for循环中使用了try-catch块.
题:
1.在for循环中有一个try-catch是不好的做法吗?
2.如果是,什么是更好的选择?
我的代码:
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
try
{
file.Delete();
}
catch(System.IO.IOException)
{
Console.WriteLine("Please Close the following File {0}", file.Name);
}
}
Run Code Online (Sandbox Code Playgroud) package Person;
sub new
{
my $class = shift;
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
# Print all the values just for clarification.
print "First Name is $self->{_firstName}\n";
print "Last Name is $self->{_lastName}\n";
print "SSN is $self->{_ssn}\n";
bless $self, $class;
return $self;
}
Run Code Online (Sandbox Code Playgroud)
我从这个网站得到了上面的代码.
从Classes官方Perl站点:
Perl没有为类定义提供任何特殊语法.包只是一个包含变量和子例程的命名空间.
考虑到这一点,上面的代码来自Tutorial Point,这是在Perl中创建类时的最佳实践吗?
在其他OO语言中,当没有设置值时,你可以从构造函数内部抛出异常,在Perl中也可以这样做吗?
哪个是正确的祝福方式?
教程点中的代码执行此操作:
bless $self, $class;
Run Code Online (Sandbox Code Playgroud)
但是从perl文档:
my $self = bless {
path => $path,
data => $data,
}, $class;
return $self;
Run Code Online (Sandbox Code Playgroud)