简体   繁体   中英

Best practices in handling java 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..?

Q: "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.

Q: Whether or not to include 3-4 statement in try block and catch exception..

The number of lines 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

Check out this trail from the Java tutorial: http://download.oracle.com/javase/tutorial/essential/exceptions/index.html (Note that this is for Java 7).

Best practices in exception handling cannot really be expressed in terms of code proportions. First learn the important difference between errors, exceptions and runtime exceptions. Errors and exceptions are two separate branches of the Throwable hierarchy. Runtime exceptions are special exception types that don't require declaration since they're not expected to be dealt with by ordinary code. Once you understand what each does, you're well underway.

Keep in mind that exception handling is not just best practices and common sense, but also influenced to some degree by style and opinion. Some people like to catch exceptions early, leading to lots of try-catch blocks in the code. Others like to surround larger numbers of lines with a try.

If you're at liberty to use JDK 7 instead of an older Java version, do investigate the try-with-resources construct and the multi-catch mechanism, both of which will help in making exception handling more elegant and reducing code bloat.

I wouldn't count lines.

a try catch block succeeds as a block of code or fails as a block of code (in generalities). So you can consider a 'unit of work' to be encompassed in one try/catch block. This is the same basic definition of a method. So the two often can go together.

Put one try/catch block in one method.

A Beginner's mistake (i think) is to create tons of individual try catch blocks, almost to tbe point of one per line. Usually you just want one big one, that encompasses the unit of work you are trying to perform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM