12.01.2014 Aufrufe

2-up - ETH Zürich

2-up - ETH Zürich

2-up - ETH Zürich

MEHR ANZEIGEN
WENIGER ANZEIGEN

Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.

YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.

Exceptions – „offizielle“ Erläuterungen<br />

• “An exception is an event, which occurs during the<br />

execution of a program, that disr<strong>up</strong>ts the normal<br />

flow of the program's instructions."<br />

• "When an error occurs within a method, the<br />

method creates an object and hands it off<br />

to the runtime system. The object, called an<br />

exception object, contains information about<br />

the error, including its type and the state of<br />

the program when the error occurred."<br />

• "Creating an exception object and handing it to the<br />

runtime system is called throwing an exception."<br />

Source: http://download.oracle.com/javase/tutorial/essential/exceptions/<br />

321<br />

Ausnahmeereignisse (Exceptions)<br />

• Ausnahmeereignisse<br />

als Fehlerereignisse<br />

• Werden oft vom System<br />

ausgelöst („throw“)<br />

• Können aber auch explizit im<br />

Programm ausgelöst werden<br />

• Sollten abgefangen und<br />

behandelt werden („catch“)<br />

• Programmstrukturierung<br />

durch „try“ und „catch“:<br />

• Fehlerbehandlung muss auf<br />

diese Weise nicht mit dem<br />

„normalen“ Programmcode<br />

verwoben werden<br />

readFile() {<br />

try {<br />

// open the file;<br />

// determine its size;<br />

// allocate that much memory;<br />

// read the file into memory;<br />

// close the file; }<br />

catch (FileOpenFailed) {<br />

// doSomething; }<br />

catch (SizeDeterminationFailed){<br />

// doSomething; }<br />

catch (MemoryAllocationFailed){<br />

// doSomething; }<br />

catch (ReadFailed){<br />

// doSomething; }<br />

catch (FileCloseFailed){<br />

// doSomething; }<br />

}<br />

von Exceptions:<br />

Weitere Nutzen von Exceptions:<br />

http://tinyurl.com/5t9uesp<br />

322<br />

135

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!