Monday, 7 April 2008

Exception Example

class NewExceptionTest {
public static void main(String args[]) {
try {
startInstall();
copyFiles();
} catch (SpaceException e) {
System.out.println("error : " + e.getMessage());
e.printStackTrace();
System.out.println("not enough space.");
} catch (MemoryException me) {
System.out.println("error : " + me.getMessage());
me.printStackTrace();
System.gc(); //expand memory from Garbage Collection
System.out.println("install again.");
} finally {
deleteTempFiles(); // delete temp files
} // end of try
} // end of main

static void startInstall() throws SpaceException, MemoryException {
if(!enoughSpace()) // if not enought space...
throw new SpaceException("not enought space.");
if (!enoughMemory()) // if not memory space..
throw new MemoryException("not enought memory space.");
} // end of startInstall

static void copyFiles() { /* copy code. */ }
static void deleteTempFiles() { /* delete temp files code .*/}

static boolean enoughSpace() {
// code for checking enough space.
return false;
}
static boolean enoughMemory() {
// code for checking memory space.
return true;
}
} // end of ExceptionTest

class SpaceException extends Exception {
SpaceException(String msg) {
super(msg);
}
}

class MemoryException extends Exception {
MemoryException(String msg) {
super(msg);
}
}

No comments: