Код: Выделить всё
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 должны быть ТОЛЬКО те строки, которые действительно могут генерировать перехватываемое вами исключение. Код внутри предложения 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;
}
Подробнее здесь: https://stackoverflow.com/questions/428 ... he-try-cla