Производительность Java try / catch, рекомендуется ли свести к минимуму то, что находится внутри предложения try?

Учитывая, что у вас есть такой код:

doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.

Теперь я знаю, что на самом деле происходит снижение производительности при создании исключения, особенно при раскручивании стека . И я также прочитал несколько статей, указывающих на небольшое снижение производительности при вводе блоков try / catch, но ни одна из статей, похоже, ни к чему не приводит.

У меня вопрос, рекомендуется ли оставлять строки внутри try catch незащищенными. минимум?, т.е. в предложении try должны быть ТОЛЬКО строки, которые могут фактически вызвать перехватываемое вами исключение. Код в предложении try работает медленнее или вызывает снижение производительности?.

Но более важно то, что это лучший метод / более удобочитаемое решение с учетом этого:

try {
    doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.
}
catch (MyCheckedException e) {
   //handle it
}

или:

try {
    doSomething() // this method may throw a checked a exception
}
catch (MyCheckedException e) {
   //Store my exception in a Map (this is all running in a loop and I want it to   continue running, but I also want to know which loops didn't complete and why)
   continue;     
} 
 //do some assignements calculations
try {
    doAnotherThing() // this method may throw a checked a exception
}
catch (MyCheckedException e) {
    //Store my exception in a Map (this is all running in a loop and I want it to   continue running, but I also want to know which loops didn't complete and why)
   continue;
} 

Это с учетом того, что вы, конечно же, будете обрабатывать ВСЕ эти отмеченные исключения точно так же.

33
задан Oscar Gomez 25 November 2010 в 21:19
поделиться