Java exception handling guidelines

I am starting to learn Java and write my first utility classes in java, which are supposed to be put into production. I am a little lost when starting to eliminate exceptions. Is there any ball-digit number about how many try arguments are in the given lines of code?

How many pieces of code should deal with exceptions .. any plugin for Eclipse?

Is it better to include 3-4 statements in a try and catch catch block, or include 10-12 lines in a try block, and then include 2-3 catch statements that catch different exceptions, for example, throw related files with either my classes or some other third-party classes ..? The first is a bit unpleasant for the eyes, and this greatly inflates the code.

Is it just common practice to just surround this code in a try block that can throw an exception or mark perfectly along the surrounding code, and also inside try say how the file descriptor is used, etc.

Any pointers ..?

+5
source share
3 answers

"It's best to include 3-4 statements in a try and catch catch block or ..."

I think you need to be clear about Exceptions in the first place.

try {
    ...some code that throws exceptions
} catch (Exception ex){
    ex.printStacktrace();
}

The above excerpt help snippet. In exception handling, we save code that can throw an exception in the block try{}, and if statemnet in the try block throws an exception, it will be caught by the block catch{}.

3-4 try catch... . .


Java Exceptions: http://marakana.com/bookshelf/java_fundamentals_tutorial/exceptions.html

Java, QA.

Java #

http://www.wikijava.org/wiki/10_best_practices_with_Exceptions

+2

Java: http://download.oracle.com/javase/tutorial/essential/exceptions/index.html ( , Java 7).

. , . - Throwable. , , . , , .

, - , . , try-catch . .

JDK 7 Java, try-with-resources , .

+1

.

catch try ( ). , " ", try/catch. . , .

try/catch .

( ) , catch catch, . , , .

0
source

All Articles