如何处理Flickr异常

时间:2013-04-23 20:31:25

标签: java file exception flickr

我有一张从Flickr网站上抓取的图片ID列表

我需要获取其位置(纬度和经度)

这是我的代码

File source = //images id 
File target = // images location

Scanner scanner = null;
PrintStream pStream =null;
String apiKey = //;
Flickr flick = new Flickr(apiKey);

PhotosInterface photosInterface=    flick.getPhotosInterface();
try {
    scanner= new Scanner(source);
    pStream= new PrintStream(target);       
    while (scanner.hasNext())
    {       
        String line = scanner.nextLine();       
        if (photosInterface.getPhoto(line).getGeoData()!=null)
        {
            pStream.println(Float.toString(photosInterface.getPhoto(line).getGeoData().getLatitude())+"\t"+Float.toString(photosInterface.getPhoto(line).getGeoData().getLongitude()));
        }
        else pStream.println("null");
    }           
    System.out.println(lineNumber);
}
catch (Exception e) {
}

}

在这种情况下,我想执行此指令

pStream.println("null");

以下情况:

图片不可用(FlickrException)

或图像与GeoData无关

怎么做?

1 个答案:

答案 0 :(得分:0)

我猜您需要围绕在FlickrException区块中引发try {..} catch的通话。

例如,如果photosInterface.getPhoto(line)在没有可用图片时可以抛出FlickrException,那么:

while (scanner.hasNext())
{       
    String line = scanner.nextLine();       
    try {
        if (photosInterface.getPhoto(line).getGeoData()!=null)
        {
            pStream.println(Float.toString(photosInterface.getPhoto(line).getGeoData().getLatitude())+"\t"+Float.toString(photosInterface.getPhoto(line).getGeoData().getLongitude()));
        }
        else pStream.println("null");
    } catch (FlickrException fe) {
        // You may need to check something about the exception
        // here in case it can be thrown for a different reason
        // than images are not available 
        pStream.println("null");
    }
}           
System.out.println(lineNumber);