如何避免在java中尝试catch块 [英] How to avoid many try catch blocks in java

查看:132
本文介绍了如何避免在java中尝试catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我刚刚结束的,而且只是有了要成为一个更好的方法:

  try {
JSONObject jsonObject = new JSONObject(jsonString);
int aCount = jsonObject.getInt(acount);
String devTok = jsonObject.getString(dt);
String qURL = jsonObject.getString(qu);
try {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
键qKey = KeyFactory.createKey(qu,qURL);
int dsACount =(Integer)datastore.get(qKey).getProperty(kLastKnownANumber);
//..etc ..更多尝试catch块需要
} catch(EntityNotFoundException e){
e.printStackTrace();
}
} catch(com.google.appengine.repackaged.org.json.JSONException e){
e.printStackTrace();
}

有更多的try catch块以相同的方式嵌入,所以在结束,只有一块块块。如何处理异常,Eclipse会一直要求我使用try catch块或Add throws声明来环绕。



有时我想捕捉一些例外,因为例如,如果找不到一个实体,我想打印像Entity not found这样的东西,如果JSON字符串不能被解析成一个对象,我想打印像无法解析JSON。



(我习惯于objective-c,其中是委托方法失败,或者该方法返回null,并且您已经传递了一个指针[指针] NSError对象将被填充,有没有可以了解try-catch?)

解决方案

如果所有做的事情是捕捉它们并打印堆栈跟踪,而不管异常类型如何,你可以将代码包装在一个大的try / catch块中。为了节省许多catch,您可以捕获所有异常实现的接口 java.lang.Throwable 。如果没有,你可以抓住每个类型的被检查的异常,你调用的代码抛出,并处理它们。



Eclipse不断要求你这样做,因为如果被检查的异常没有被捕获,或声明被调用者抛出,Java代码将不会被编译。



+将此注释添加到答案中(谢谢Paul Tomblin) :



在生产质量应用程序中,您将记录跟踪,添加一些逻辑,您以正确的方式处理异常,采用备用流程和/或者将其重新包装在另一个异常中并抛出它等等。这一切都取决于您要解决的特定问题。


I'm very new to java and the idea of try catch blocks to handle exceptions.

This roughly what I'm ending up with, and there simply has to be a better way:

    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        int aCount = jsonObject.getInt("acount");
        String devTok = jsonObject.getString("dt");
        String qURL = jsonObject.getString("qu");
        try {
            DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
            Key qKey = KeyFactory.createKey("qu", qURL);
            int dsACount = (Integer) datastore.get(qKey).getProperty(kLastKnownANumber);
            //..etc.. more try catch blocks needed
        } catch (EntityNotFoundException e) {
            e.printStackTrace();
        }
    } catch (com.google.appengine.repackaged.org.json.JSONException e) {
        e.printStackTrace();
    }

There are more try catch blocks embedded in the same way, so that at the end, there is just a lump of catch blocks. How else should exceptions be handled, Eclipse keeps asking my to either surround with a try catch block or "Add throws declaration".

Sometimes I want to catch certain exceptions, for example if it can't find an entity, I want to print something like "Entity not found", and if the JSON string can't be parsed into an object, I want to print something like "Can't parse JSON".

(I'm used to objective-c where the are delegate methods for failure, or the method returns null and you have passed a pointer to [a pointer to] an NSError object, which will have been "filled", Is there somewhere to learn about try-catch?)

解决方案

If all you're doing is catching them and printing the stack trace regardless of the exception type, you can just wrap the code in one large try/catch block. To save many "catches", you can catch java.lang.Throwable which is the interface that all exceptions implement. If not, you can have a catch for every type of checked exception the code you're calling throws, and handle them specifically.

Eclipse keeps asking you to do so because Java code will not compile if the checked exceptions are not caught, or declared to be thrown by the caller.

+Adding this comment to the answer (Thanks, Paul Tomblin):

In production quality apps you'd be logging the trace, adding some logic where you're handling the exception in a right way, taking an alternate flow, and/or re-wrapping it in another exception and throwing it, etc. It all depends on the particular problem you're trying to solve.

这篇关于如何避免在java中尝试catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆