解析 DBXException java 的最佳方法
Best way to parse DBXException java
所以我最近问了如何处理 Dropbox API 异常 . I learned that I would have to parse the DBXEception into its subclasses 的问题,其中有很多。现在想到这个,我想知道处理这个问题的最佳方法是什么。
目前我计划使用 instanceof 并像这样检查如果我希望程序再次尝试它将 return true 并且程序可能会再次尝试使用服务器请求的指数退避
public boolean parseDBX(DbxException e)
{
if(e instanceof AccessErrorException) {//handle Error
}else if (e instanceof DbxApiException) {//handle Error
}etc
}
它会像这样在 catch 块中调用
for(int i =0;;i++) {
try {
ListFolderResult result = client.files().listFolder("/Saves/"+prefName);
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
//metadata.
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
} catch (ListFolderErrorException e) {
createDefFolder();
} catch (DbxException e) {
if(codeHandler.parse(e)&&i<10) {
continue;
}else {
log.write("Error 5332490: a problem was encountered while trying to check for the root file"+e.getMessage());
throw new IOException();
}
}
}
所以我的问题是有没有一种方法可以改用 switch 语句(根据我的发现,答案是否定的),如果没有,是否有更好的方法来处理检查异常类型。
最好的方法是通过捕获适当类型的异常来完全避免 "parsing" 异常:
try {
...
} catch (AccessErrorException aee) {
...
} catch (DbxApiException dae) {
...
}
如果不希望这样做,您可以将自己的整数 ID 与每个异常类型相关联,将其放在 Map
中,并在 parse
方法中使用它来区分子类型一个 switch
:
static Map<Class,Integer> parseId = new HashMap<>();
static {
parseId.put(AccessErrorException.class, 1);
parseId.put(DbxApiException.class, 2);
...
}
...
public void parseDBX(DbxException e) {
Integer id = parseId.get(e.getClass());
if (id != null) {
switch (id.intValue()) {
...
}
}
}
所以我最近问了如何处理 Dropbox API 异常
目前我计划使用 instanceof 并像这样检查如果我希望程序再次尝试它将 return true 并且程序可能会再次尝试使用服务器请求的指数退避
public boolean parseDBX(DbxException e)
{
if(e instanceof AccessErrorException) {//handle Error
}else if (e instanceof DbxApiException) {//handle Error
}etc
}
它会像这样在 catch 块中调用
for(int i =0;;i++) {
try {
ListFolderResult result = client.files().listFolder("/Saves/"+prefName);
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
//metadata.
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
} catch (ListFolderErrorException e) {
createDefFolder();
} catch (DbxException e) {
if(codeHandler.parse(e)&&i<10) {
continue;
}else {
log.write("Error 5332490: a problem was encountered while trying to check for the root file"+e.getMessage());
throw new IOException();
}
}
}
所以我的问题是有没有一种方法可以改用 switch 语句(根据我的发现,答案是否定的),如果没有,是否有更好的方法来处理检查异常类型。
最好的方法是通过捕获适当类型的异常来完全避免 "parsing" 异常:
try {
...
} catch (AccessErrorException aee) {
...
} catch (DbxApiException dae) {
...
}
如果不希望这样做,您可以将自己的整数 ID 与每个异常类型相关联,将其放在 Map
中,并在 parse
方法中使用它来区分子类型一个 switch
:
static Map<Class,Integer> parseId = new HashMap<>();
static {
parseId.put(AccessErrorException.class, 1);
parseId.put(DbxApiException.class, 2);
...
}
...
public void parseDBX(DbxException e) {
Integer id = parseId.get(e.getClass());
if (id != null) {
switch (id.intValue()) {
...
}
}
}