[英]catching errors with using BinaryWriter
I am using a using with the BinaryWriter as follows: 我正在使用BinaryWriter,如下所示:
using (BinaryWriter writer = new BinaryWriter(File.Open(configData.RxOutFn, FileMode.Create)))
{
// Do Something
}
Sometimes I have the file open in another app, and the Create fails, how can I catch this error? 有时我在另一个应用程序中打开了文件,但是创建失败,如何捕获此错误?
I tried putting a try/catch around the whole thing like this: 我试图像这样尝试/抓住整个事情:
try
{
using (BinaryWriter writer = new BinaryWriter(File.Open(configData.RxOutFn, FileMode.Create)))
{
// Do something
}
}
catch
{
// Display error
}
But I am worried it will catch the wrong thing as there is lots of code in the 但是我担心它会捕获错误的信息,因为其中有很多代码
// Do Something
Any ideas how i can catch this error? 任何想法,我怎么能抓住这个错误?
Many thanks in advance 提前谢谢了
Andy 安迪
Check what exception gets thrown ( File.Open
throws an IOException
which I think is what gets thrown when the file can't be created) and catch that specific exception: 检查引发了什么异常(
File.Open
引发了IOException
,我认为这是无法创建文件时引发的异常),并捕获该特定异常:
try
{
using (BinaryWriter writer = new BinaryWriter(File.Open(configData.RxOutFn, FileMode.Create)))
{
// Do something
}
}
catch (IOException e)
{
// Display error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.