Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java异常从catch抛出对象并获取对象_Java - Fatal编程技术网

Java异常从catch抛出对象并获取对象

Java异常从catch抛出对象并获取对象,java,Java,我想在获取异常和从catch获取时传递对象。 我有UserDataExceptionextend from Exception throw new UserDataException("Media uploaded failed",mediaDO, dupkExp); mediaDO是一个对象 如果可能,从catch语句中获取此mediaDo对象 catch (UserDataException uExp) { } UserdataExceptioncalss: public cl

我想在获取异常和从catch获取时传递对象。 我有
UserDataException
extend from Exception

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);
}
}


如果可能,请提供帮助。

您必须在异常类中存储对该对象的引用,并使用getter获取它:

private MediaDO mediaDO;

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

public MediaDO getMediaDO()
{
    return mediaDO;
}
然后在catch块中:

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

你可以在exception类中提供一个getter方法。有什么建议可以这样做吗?我的意思是这样做是一种好的做法,在抛出异常时传递回大对象。