C# 用户创建的异常类
我必须为.NET项目创建自己的异常类。有人能帮我在代码中如何使用它吗?我只是想向MessageBox用户显示没有找到图像。但我不知道在哪里做。希望得到一个好的答案C# 用户创建的异常类,c#,.net,exception,C#,.net,Exception,我必须为.NET项目创建自己的异常类。有人能帮我在代码中如何使用它吗?我只是想向MessageBox用户显示没有找到图像。但我不知道在哪里做。希望得到一个好的答案 class RijException : Exception { public RijException() : base() { } public RijException(string message) : base(message) { } public RijExce
class RijException : Exception
{
public RijException()
: base() { }
public RijException(string message)
: base(message) { }
public RijException(string format, params object[] args)
: base(string.Format(format, args)) { }
public RijException(string message, Exception innerException)
: base(message, innerException) { }
public RijException(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }
}
现在我想使用它:
try
{
afbeeldingPictureBox.BackgroundImage =
Image.FromFile(@"..\..\Borden\Stilstaan en parkeren\Parkeren toegelaten.png");
}
catch (FileNotFoundException)
{
throw new RijException("Can't find the images");
}
编辑
try
{
try
{
afbeeldingPictureBox.BackgroundImage = Image.FromFile(@"..\..\Borden\Stilstaan en parkeren\Parkeren toegelaten.png");
}
catch (FileNotFoundException)
{
throw new RijException("ImageNotFound");
//MessageBox.Show("Afbeeldingen konden niet worden geladen");
}
}
catch (RijException)
{
MessageBox.Show("Not able to load the image...");
}
一般来说,抓住一个你无能为力的例外情况并不是一个好主意。仅仅捕获
FileNotFoundException
,然后抛出一个本质上传递相同信息的新rijeexception
,通常不会这样做
我只是想向MessageBox用户显示没有找到图像。但我不知道在哪里做
重组代码以显示消息框的最直接方法是
catch (FileNotFoundException)
{
MessageBox.Show("Can't find the images.");
//Don't do this: throw new RijException("Can't find the images");
}
您确实不希望自定义的
RijException
承担显示消息框的责任
捕获您无法处理的异常通常不是一个好主意。仅仅捕获FileNotFoundException
,然后抛出一个本质上传递相同信息的新rijeexception
,通常不会这样做
我只是想向MessageBox用户显示没有找到图像。但我不知道在哪里做
重组代码以显示消息框的最直接方法是
catch (FileNotFoundException)
{
MessageBox.Show("Can't find the images.");
//Don't do this: throw new RijException("Can't find the images");
}
<>你真的不希望你的习惯>代码> RijExabue/Cult>负责显示<代码>消息框< /> > 可以改进你的例子,但是在这之前我们先举一个更有用的例子。 假设您有以下代码:
class Program
{
static void Main(string[] args)
{
try
{
var processor = new FileProcessor();
processor.Run();
}
catch (RijException exception)
{
//what will I get here.
}
}
}
它使用以下类:
public class FileProcessor
{
private string _myFileName;
public void Run()
{
try
{
var fileLoader = new FileLoader();
Process(fileLoader.Load(_myFileName));
}
catch (FileNotFoundException)
{
throw new RijException("Can't find requested file");
}
}
private void Process(object file)
{
//some logic
}
}
public class FileLoader
{
public object Load(string myFileName)
{
//throws FileNotFoundException
}
}
因此,调用堆栈将如下所示:
try
{
var filename = @"..\..\Borden\Stilstaan en parkeren\Parkeren toegelaten.png";
try
{
afbeeldingPictureBox.BackgroundImage = Image.FromFile(filename);
}
catch (FileNotFoundException exception)
{
throw new RijException("Failed to load " + filename, exception);
}
}
catch (RijException)
{
MessageBox.Show("Not able to load the image...");
//now you would have all information required
//to figure out where and why something went wrong.
}
如果我们修改main方法中的代码来打印堆栈跟踪,您认为我们得到了什么
try
{
var processor = new FileProcessor();
processor.Run();
}
catch (RijException exception)
{
Console.WriteLine(exception.StackTrace);
}
正确答案是:
看到了吗?堆栈跟踪显示错误发生在FileProcessor
中,而它实际上发生在FileLoader
中。原因是此代码:
catch (FileNotFoundException)
{
throw new RijException("Can't find requested file");
}
当捕获一个异常并抛出另一个异常时,必须始终将原始异常作为内部异常包含在内。否则,很难理解异常最初发生在何处
catch (FileNotFoundException ex)
{
throw new RijException("Can't find requested file", ex); //includes inner
}
另一个问题是异常消息:“找不到请求的文件”
。问问你自己。如果你在一个日志文件中得到这个消息,你能找出哪里出了问题吗?至少提供一些上下文信息
最后你还有一个问题。设计异常的最佳实践表明,它们应该能够被序列化。为此,需要包含序列化构造函数并将异常标记为可序列化:
[Serializable]
public class RijException : Exception
{
public RijException(string message) : base(message)
{
}
public RijException(string message, Exception inner) : base(message, inner)
{
}
//serialization constructor
protected RijException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
最后,我会这样写你的练习:
try
{
var filename = @"..\..\Borden\Stilstaan en parkeren\Parkeren toegelaten.png";
try
{
afbeeldingPictureBox.BackgroundImage = Image.FromFile(filename);
}
catch (FileNotFoundException exception)
{
throw new RijException("Failed to load " + filename, exception);
}
}
catch (RijException)
{
MessageBox.Show("Not able to load the image...");
//now you would have all information required
//to figure out where and why something went wrong.
}
摘要
<>你的例子可以改进,但在这之前我们先举一个更有用的例子。< /P> 假设您有以下代码:
class Program
{
static void Main(string[] args)
{
try
{
var processor = new FileProcessor();
processor.Run();
}
catch (RijException exception)
{
//what will I get here.
}
}
}
它使用以下类:
public class FileProcessor
{
private string _myFileName;
public void Run()
{
try
{
var fileLoader = new FileLoader();
Process(fileLoader.Load(_myFileName));
}
catch (FileNotFoundException)
{
throw new RijException("Can't find requested file");
}
}
private void Process(object file)
{
//some logic
}
}
public class FileLoader
{
public object Load(string myFileName)
{
//throws FileNotFoundException
}
}
因此,调用堆栈将如下所示:
try
{
var filename = @"..\..\Borden\Stilstaan en parkeren\Parkeren toegelaten.png";
try
{
afbeeldingPictureBox.BackgroundImage = Image.FromFile(filename);
}
catch (FileNotFoundException exception)
{
throw new RijException("Failed to load " + filename, exception);
}
}
catch (RijException)
{
MessageBox.Show("Not able to load the image...");
//now you would have all information required
//to figure out where and why something went wrong.
}
如果我们修改main方法中的代码来打印堆栈跟踪,您认为我们得到了什么
try
{
var processor = new FileProcessor();
processor.Run();
}
catch (RijException exception)
{
Console.WriteLine(exception.StackTrace);
}
正确答案是:
看到了吗?堆栈跟踪显示错误发生在FileProcessor
中,而它实际上发生在FileLoader
中。原因是此代码:
catch (FileNotFoundException)
{
throw new RijException("Can't find requested file");
}
当捕获一个异常并抛出另一个异常时,必须始终将原始异常作为内部异常包含在内。否则,很难理解异常最初发生在何处
catch (FileNotFoundException ex)
{
throw new RijException("Can't find requested file", ex); //includes inner
}
另一个问题是异常消息:“找不到请求的文件”
。问问你自己。如果你在一个日志文件中得到这个消息,你能找出哪里出了问题吗?至少提供一些上下文信息
最后你还有一个问题。设计异常的最佳实践表明,它们应该能够被序列化。为此,需要包含序列化构造函数并将异常标记为可序列化:
[Serializable]
public class RijException : Exception
{
public RijException(string message) : base(message)
{
}
public RijException(string message, Exception inner) : base(message, inner)
{
}
//serialization constructor
protected RijException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
最后,我会这样写你的练习:
try
{
var filename = @"..\..\Borden\Stilstaan en parkeren\Parkeren toegelaten.png";
try
{
afbeeldingPictureBox.BackgroundImage = Image.FromFile(filename);
}
catch (FileNotFoundException exception)
{
throw new RijException("Failed to load " + filename, exception);
}
}
catch (RijException)
{
MessageBox.Show("Not able to load the image...");
//now you would have all information required
//to figure out where and why something went wrong.
}
摘要
我不知道在哪里对messageBox进行编码。将
抛出新的…
更改为messageBox.Show()
?最终捕获异常的位置。还要检查您是否需要将异常类设置为public
我不知道在哪里对messageBox进行编码。将抛出新的…
更改为messageBox.Show()
?最终捕获异常的位置。还要检查是否需要将异常类设置为公共
使用异常类。。。到底要做什么?与自定义异常相关的具体要求是什么?我必须在这里编写自己的异常类RijException。编辑后请你看一下代码好吗。好还是不好?@gurpret.S:虽然捕获并重新捕获自定义异常是一种不好的方式,但我认为这是一种学习练习。基于这种假设,您的更新代码演示了自定义异常的原理,应该可以正常工作。为什么它是一种不好的样式?我宁愿说,这取决于用途case@jgauffin:我想不出哪一个用例捕捉到我无能为力的异常是有意义的,只是为了重现一个传达基本相同信息的不同异常。如果您可以对此做些什么(在应用程序和需求的上下文中),那么它是有意义的。。。到底要做什么?与您的定制例外情况相关的具体要求是什么