Skip to content
Home » Gestión de Excepciones

Gestión de Excepciones

Desde java.sun.com: una de las razones por la que la inminente aparición de la versión definitiva de ZE2 tiene a muchos desarrolladores de PHP al border de su asiento.

¿Que son las excepciones y por qué deberías tener cuidado? Una gestión adecuada de una excepción te permite cambiar tu código desde algo como esto:

errorCodeType readFile {
initialize errorCode = 0;
open the file;
if (theFileIsOpen) {
determine the length of the file;
if (gotTheFileLength) {
allocate that much memory;
if (gotEnoughMemory) {
read the file into memory;
if (readFailed) {
errorCode = -1;
}
} else {
errorCode = -2;
}
} else {
errorCode = -3;
}
close the file;
if (theFileDidntClose && errorCode == 0) {
errorCode = -4;
} else {
errorCode = errorCode and -4;
}
} else {
errorCode = -5;
}
return errorCode;
}

a esto:

readFile {
try {
open the file;
determine its size;
allocate that much memory;
read the file into memory;
close the file;
} catch (fileOpenFailed) {
doSomething;
} catch (sizeDeterminationFailed) {
doSomething;
} catch (memoryAllocationFailed) {
doSomething;
} catch (readFailed) {
doSomething;
} catch (fileCloseFailed) {
doSomething;
}
}

Este excelente artículo de Sun explica el manejo de excepciones en detalle y te añadirá a la creciente lista de los que esperan ZE2 😉

El artículo completo está disponible aquí