10.11.2014 Views

Exception Handling in Java

Exception Handling in Java

Exception Handling in Java

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The f<strong>in</strong>ally block<br />

A f<strong>in</strong>ally block is where you put code that must run regardless of an exception.<br />

try {<br />

// Dangerous activities that might throw A or B }<br />

catch(A a1) {<br />

// Handler for situation A}<br />

catch(B b1) {<br />

// Handler for situation B}<br />

f<strong>in</strong>ally {<br />

// Activities that happen every time<br />

}<br />

The f<strong>in</strong>ally block is a key tool for prevent<strong>in</strong>g resource leaks. When clos<strong>in</strong>g a file or otherwise<br />

recover<strong>in</strong>g resources, place the code <strong>in</strong> a f<strong>in</strong>ally block to <strong>in</strong>sure that resource is always recovered.<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!