处理java异常的最佳实践 [英] Best practices in handling java exceptions
问题描述
应该用多少部分代码来处理exception..any插件Eclipse?
最好的做法是在try块中包含3-4个语句并捕获异常,或者在try块中包含10-12行,然后包含2-3捕获不同种类的异常的catch语句说的是由File相关的或由我自己的类或其他第三方类引发的。
前者对眼睛有点不满意,这是令人兴奋的代码这么多..
这种常见的做法只有和只有环绕该代码在try块这可以抛出异常,或者标记周围的代码也很好,尝试说如何使用文件句柄等。
任何指针..?
最好的做法是在try块中包含3-4个语句,并捕获异常或... strong>
我想你需要先了解例外情况。
try {
...一些引发异常的代码
} catch(Exception ex){
ex.printStacktrace();
}
上面是处理异常的代码段。在异常处理中,我们保留可能在 try {}
块中生成异常的代码,如果try块中的statemnet生成异常,它将被 catch {}
块
是否在try块和catch异常中包含3-4条语句。 > ..不是由规则决定的。这取决于你的逻辑和程序的要求。
以下链接可帮助您清除有关Java异常的基金: http://marakana.com/bookshelf/java_fundamentals_tutorial/exceptions.html
有关Java异常的最佳做法,请遵循以下文章和QA。
http://www.wikijava.org/wiki/10_best_practices_with_Exceptions
I am beginning to learn Java and writing my first utility classes in java which are supposed to go in production. I am somewhat lost when it is coming to dealing with exceptions. Is there some ballpark figure on how many try statements are there in a given lines of code?
How much fraction of code is supposed to deal with exceptions..any plugin for Eclipse?
Is it best practice to include 3-4 statements in a try block and catch exception or include 10-12 lines in a try block and afterwards include 2-3 catch statements catching different kind of exceptions say ones throws by File related or by my own classes or some other 3rd party classes..? The former is a bit displeasing to eyes and it is bloating code so much..
Is this common practice to only and only surround that code in try block which can throw exception or it's fine to tag along surrounding code as well inside try say how the file handle is being used etc..
Any pointers..?
"Is it best practice to include 3-4 statements in a try block and catch exception or ... "
I think you need to be clear about Exceptions first.
try {
...some code that throws exceptions
} catch (Exception ex){
ex.printStacktrace();
}
The above is snippet of handling an exception. In exception handling, we keep the code that might generate exception in try{}
block, and if the statemnet in try block generates exception it will be caught by catch{}
block.
Whether or not to include 3-4 statement in try block and catch exception... is not determined by RULES. It depends upon your logic and requirement of program.
The following link helps you to clear funda about Java Exceptions : http://marakana.com/bookshelf/java_fundamentals_tutorial/exceptions.html
For best practices about Java Exceptions, Follow following articles and QA.
Best practices for exception management in Java or C#
http://www.wikijava.org/wiki/10_best_practices_with_Exceptions
这篇关于处理java异常的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!