2015-05-14 86 views
1

我想在獲取異常並從catch中獲取時傳遞對象。 我有UserDataException自Exception延伸Java異常拋出對象並從catch中獲取對象

throw new UserDataException("Media uploaded failed",mediaDO, dupkExp); 

mediaDO是對象。

如果可能的話,從catch說法得到這個mediaDo對象

catch (UserDataException uExp) { 

    } 

UserdataException CALSS:

public class UserDataException extends Exception { 

private static final String ERROR = "Database Transaction Error: "; 
private static final String DKERROR = "Duplicate Key Error"; 

/** 
* 
*/ 
private static final long serialVersionUID = 6209976874559850953L; 

/** 
* 
*/ 
public UserDataException(String message, Throwable throwable) { 
    super(message, throwable); 
} 

public UserDataException(String message) { 
    super(message); 
} 

/** 
* <code>UserDataException</code> thrown with message and throwable object 
* 
* @param schdulerData 
* @param message 
* @param throwable 
*/ 
public UserDataException(String message, SchedulerData schdulerData, final Throwable throwable) { 

    super(ERROR + message, throwable); 
} 

/** 
* <code>UserDataException</code> thrown with message and throwable object 
* 
* @param schdulerData 
* @param message 
* @param throwable 
*/ 
public UserDataException(String message, MediaDO mediaDO, final Throwable throwable) { 

    super(DKERROR + message, throwable); 
} 

}

如果可能的話,請幫助..

+0

ü可以提供異常類中的getter方法.. –

回答

5

您必須存儲對該對象的引用在catch塊

private MediaDO mediaDO; 

public UserDataException(String message, MediaDO mediaDO, final Throwable throwable) { 
    super(DKERROR + message, throwable); 
    this.mediaDO = mediaDO; 
} 

public MediaDO getMediaDO() 
{ 
    return mediaDO; 
} 

然後:您的異常類,並使用一個getter得到它

catch (UserDataException uExp) { 
    MediaDO mediaDO = uExp.getMediaDO(); 
    ... 
}