最后捕获或返回实例或null引用

时间:2012-07-31 12:27:39

标签: java exception exception-handling error-handling javax.mail

使用GNU NNTP,如何让getMessage(String newsgroup, int i)使用真正的javax.mail.Message或null Message返回?

init:
Deleting: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
Deleted 1 out of date files in 0 seconds
Compiling 2 source files to /home/thufir/NetBeansProjects/USENET/build/classes
warning: Supported source version 'RELEASE_6' from annotation processor 'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor' less than -source '1.7'
warning: The following options were not recognized by any processor: '[eclipselink.canonicalmodel.use_static_factory]'
1 warning
Copying 1 file to /home/thufir/NetBeansProjects/USENET/build/classes
compile:
run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 6 groups in 31ms
[EL Info]: 2012-07-31 05:21:51.235--ServerSession(1478593)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-07-31 05:21:52.523--ServerSession(1478593)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
[EL Warning]: 2012-07-31 05:21:52.672--ServerSession(1478593)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'usenet.HEADERFIELD' doesn't exist
Error Code: 1146
Call: ALTER TABLE HEADERFIELD DROP FOREIGN KEY FK_HEADERFIELD_ARTICLE_ID
Query: DataModifyQuery(sql="ALTER TABLE HEADERFIELD DROP FOREIGN KEY FK_HEADERFIELD_ARTICLE_ID")
[EL Warning]: 2012-07-31 05:21:52.687--ServerSession(1478593)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'ARTICLE'
Error Code: 1051
Call: DROP TABLE ARTICLE
Query: DataModifyQuery(sql="DROP TABLE ARTICLE")
[EL Warning]: 2012-07-31 05:21:52.797--ServerSession(1478593)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'HEADERFIELD'
Error Code: 1051
Call: DROP TABLE HEADERFIELD
Query: DataModifyQuery(sql="DROP TABLE HEADERFIELD")
Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: [gwene.ca.craigslist.vancouver.labour.general, gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader, gwene.com.economist]
Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 5
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 6
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 7
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 8
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 9
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 10
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 9
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

Jul 31, 2012 5:21:53 AM net.bounceme.dur.usenet.model.Usenet getMessage
SEVERE: null
javax.mail.MessageRemovedException: No such article: 10
    at gnu.mail.providers.nntp.NNTPFolder.getMessage(NNTPFolder.java:330)
    at net.bounceme.dur.usenet.model.Usenet.getMessage(Usenet.java:72)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:40)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:24)

BUILD SUCCESSFUL (total time: 10 seconds)

枚举:

package net.bounceme.dur.usenet.model;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;

public enum Usenet {

    INSTANCE;
    private final Logger LOG = Logger.getLogger(Usenet.class.getName());
    private Properties props = new Properties();
    private Folder root = null;
    private Store store = null;
    private List<Folder> folders = new ArrayList<>();
    private Folder folder = null;

    Usenet() {
        LOG.fine("controller..");
        props = PropertiesReader.getProps();
        try {
            connect();
        } catch (Exception ex) {
            Logger.getLogger(Usenet.class.getName()).log(Level.SEVERE, "FAILED TO LOAD MESSAGES", ex);
        }
    }

    private void connect() throws Exception {
        LOG.fine("Usenet.connect..");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);
        store = session.getStore(new URLName(props.getProperty("nntp.host")));
        store.connect();
        root = store.getDefaultFolder();
        setFolders(Arrays.asList(root.listSubscribed()));
    }

    public void foo(String ng) throws Exception {
        folder = root.getFolder(ng);
        LOG.fine("opened the folder!!!!!");
        folder.open(Folder.READ_ONLY);
    }

    public List<Message> getMessages(String newsgroup) throws Exception {
        LOG.fine("fetching.." + newsgroup);
        folder = root.getFolder(newsgroup);
        LOG.fine("opened the folder!!!!!");
        folder.open(Folder.READ_ONLY);
        LOG.fine("opened: " + folder.getFullName());
        LOG.fine("opened: " + folder.getFullName());
        LOG.fine("opened: " + folder.getFullName());
        List<Message> messages = Arrays.asList(folder.getMessages());
        LOG.fine("..fetched " + folder);
        return messages;
    }

    public List<Folder> getFolders() {
        LOG.fine("folders " + folders);
        return Collections.unmodifiableList(folders);
    }

    private void setFolders(List<Folder> folders) {
        this.folders = folders;
    }

    public Message getMessage(String newsgroup, int i) {
        Message message = null;
        try {
            LOG.fine("fetching.." + newsgroup);
            folder = root.getFolder(newsgroup);
            folder.open(Folder.READ_ONLY);
            return folder.getMessage(i);
        } catch (MessagingException ex) {
            Logger.getLogger(Usenet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return message;//crummy
        }
    }
}

如何处理此异常以便程序不会崩溃?

1 个答案:

答案 0 :(得分:1)

在尝试处理它之前,你做了一个简单的布尔检查:

if(!message.isExpunged)  
{  
     //process your message here  
}    
else{
    //log a message about it being expunged  
}

API

public boolean isExpunged()

检查此消息是否已清除。除了getMessageNumber()之外的所有其他方法在已清除的Message对象上都是无效的。

由于对包含文件夹的explict expunge()请求而被清除的邮件会立即从文件夹中删除。由另一个源外部清除的邮件被标记为“已清除”,并且对于isExpunged()方法返回true,但是在对文件夹进行显式删除()之前,它们不会从文件夹中删除。

有关清除处理的更多详细信息,请参阅expunge()的说明。