THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
There are plenty of wisdom out on the internet regarding exceptions and handling. Here is some general anti-patterns and, on the bottom of that page, there are resources to other guidelines. There are a few that I like to single out as important.
Code Block |
---|
try { code...; } catch (Exception specific to your code) { Specific exception handling and logging; } catch (Exception e) { s_logger.warn("Caught unexpected exception", e); exception handling code. } |
|
Code Block |
---|
try { code...; } catch (XenAPIException e) { // Do either this: s_logger.warn("Caught a xen api exception", e); // or throw new CloudRuntimeException("Caught a xen api exception", e); throw new CloudRuntimeException("Got a xen api exception"); // Don't ever do JUST this. } |
|
Code Block |
---|
public void irresponsibleMethod() throws Exception; public void responsibleMethod() throws XenAPIException; public void runtimeExceptMethod(); // throws CloudRuntimeException that's not suppose to be logged until entry point. public void innocentCaller() { try { irresponsibleMethod(); responsibleMethod(); runtimeExceptionMethod(); } catch(Exception e) { s_logger.warn("Unable to execute", e); throw new CloudRuntimeException("Unable to execute", e); // What's wrong here? // 1. If the error was thrown from responsibleMethod, the caller now forgot to do special handling for XenAPIException. // 2. If the error was thrown from runtimeExceptionMethod, the caller now log it once here, and will log again at entry point. } |
Code Block |
---|
try { some code; } catch(XenAPIException e) { // catch generic error here. s_logger.debug("There's an exception. Rolling back code: " + e.getMessage()); ...rollback some code; throw e; // note there's no "new" here. } |
Code Block |
---|
for (Task task : taskList) { try { process task; } catch (Exception e) { ...handle exception and continue } } |
CloudStack do have a list of well known exceptions and there are some exceptions are important to describe here.
...