简体   繁体   English

如何拦截C ++中ImageMagick引发的异常?

[英]How do I intercept an exception thrown by ImageMagick in C++?

I am trying to retrieve photos from the internet with ImageMagick. 我正在尝试使用ImageMagick从互联网上检索照片。 Once in a while there will be one with problems. 偶尔会有一个问题。 How do I handle that? 我该如何处理?

char file[] = "http://distilleryimage10.s3.amazonaws.com/1f6be58e383e11e3acaf22000ae80c8d_8.jpg";

Magick::Image image;
// use Magick to load the file
try {
  image.read(file);
}
catch(int err) {
  printf("Error retrieving snapshot. Skipping.\n");
  return;
}
/* ... use this image */

That particular URL, for example has restricted access. 例如,该特定URL的访问受到限制。 ImageMagick just throws an exception and says: ImageMagick只是抛出一个异常并说:

terminate called after throwing an instance of 'Magick::ErrorCoder'
  what():  Magick: no data returned `http://distilleryimage10.s3.amazonaws.com/1f6be58e383e11e3acaf22000ae80c8d_8.jpg' @ error/url.c/ReadURLImage/232
Aborted

I thought my try/catch would capture that, but I have more experience with try/except from python. 我以为我的try / catch可以捕获到这一点,但是我从python获得try / except的更多经验。 I would expect other things could cause faults, too, such as 404's or 500's. 我希望其他事情也可能导致故障,例如404或500。

What can I do? 我能做什么?

You are trying to catch an int , which is not what ImageMagick throws. 您正在尝试捕获一个int ,这不是ImageMagick引发的。 The actual exception class is indicated in your error message: Magick::ErrorCoder . 错误消息中指示了实际的异常类: Magick::ErrorCoder

You could either catch this very exception type: 您可以捕获此异常类型:

try {
  image.read(file);
}
catch(Magick::ErrorCoder& err) {
  //...
};

or consult the ImageMagick documentation and catch a base class of this one. 或查阅ImageMagick文档并获取该文档的基类。

In general you should catch by std::exception at a minimum. 通常,您至少应注意std :: exception。 Any sensible library will derive its exception classes from std::exception - that's what it's for. 任何明智的库都将从std :: exception派生其异常类-这就是它的作用。

The what() method of std::exception will give you some hint as to what the exception is about. std :: exception的what()方法将向您提示有关异常的含义。 Googling for Magick::ErrorCoder yields the Doxygen documentation which indeed shows it's derived from std::exception: http://www.imagemagick.org/api/Magick++/classMagick_1_1ErrorCoder.html 搜寻Magick :: ErrorCoder会产生Doxygen文档,该文档确实显示它是从std :: exception派生的: http : //www.imagemagick.org/api/Magick++/classMagick_1_1ErrorCoder.html

As syam suggests, since you know that Magick::ErrorCoder exceptions are emitted when things go wrong than you should catch those and possibly the intermediate exception classes from which it derives, but always catch std::exception as that will allow your program to report any sensible c++ exception thrown by your library. 正如syam所建议的那样,因为您知道发生错误时会发出Magick :: ErrorCoder异常,所以您应该捕获那些异常以及可能派生自其的中间异常类,但始终要捕获std :: exception,因为这将使您的程序能够报告库抛出的任何明智的c ++异常。

try 
{
    image.read(file);
}
catch(Magick::ErrorCoder const & err) 
{
    // Some specific error handling for this problem
}
catch(Magick::Error const & err) 
{
    // Some general handling for ImageMagick errors
}
catch(Magick::Exception const & err) 
{
    // Some general handling for ImageMagick errors/warnings (apparently)
}
catch(std::exception const & err) 
{
    // Something bad happened - possibly caused by imagemagick using its libraries
    // incorrectly. Just report it - at least we didn't bomb out:
    std::cout << err.what();
}

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

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