Java

try-with-resources

Në shqip përdoret i njëjti emër.

ShpjegimiSQ

try-with-resources e hap një burim — skedar, lidhje me databazën, Scanner — dhe e mbyll vetë kur mbaron blloku, edhe nëse ndodh gabim. Është si with-i i Python-it.

EnglishEN

try-with-resources opens a resource — a file, a database connection, a Scanner — and closes it automatically when the block ends, even if an error happens. It's like Python's with.

Si ta mendosh

Si drita e korridorit me kohëmatës: ndizet kur hyn dhe fiket vetë pasi del.

Lexoje në anglisht

Like a timed hallway light: on when you walk in, off by itself after you leave.

Shembull kodijava

try (Scanner sc = new Scanner(new File("notat.txt"))) {
    while (sc.hasNextLine()) System.out.println(sc.nextLine());
}  // skedari mbyllet vetë këtu

Terma të lidhur