R
Rhino
Have you ever had a piece of database code in which you wanted to test every
probable and improbable error condition that might occur?
For example, suppose you had this code and wanted to make sure that the
error handling did the right thing when an InstantiationException occurred:
----------------------------------------------------------------------------------------------------------------
/* Load the JDBC driver. */
try {
Class.forName(this.jdbcDriverName).newInstance();
}
catch (ClassNotFoundException cnf_excp) {
String msg = "Failed to load JDBC driver " + this.jdbcDriverName
+ ".";
this.logger.logp(Level.SEVERE, this.CLASS_NAME, METHOD_NAME,
msg, cnf_excp);
System.exit(16);
}
catch (InstantiationException i_excp) {
String msg = "Failed to load JDBC driver " + this.jdbcDriverName
+ ".";
this.logger.logp(Level.SEVERE, this.CLASS_NAME, METHOD_NAME,
msg, i_excp);
System.exit(16);
}
catch (IllegalAccessException ia_excp) {
String msg = "Failed to load JDBC driver " + this.jdbcDriverName
+ ".";
this.logger.logp(Level.SEVERE, this.CLASS_NAME, METHOD_NAME,
msg, ia_excp);
System.exit(16);
}
----------------------------------------------------------------------------------------------------------------
Well, up until a few minutes ago, I had always wondered if there was some
simple way to _force_ a desired exception, like an InstantiationException in
this case, to occur at the desired point so that I could make sure the
second catch would work.
Then, inspiration dawned. I tried adding 'throw new
InstantiantionException();' as the last statement in the try block, executed
the code, and BINGO, the InstantiationException occurred. My second catch
block was invoked and did what it was supposed to do.
Well, this is no great miracle by any stretch of the imagination and it
seems blindingly obvious as I mention it now; many of you are probably
yawning as I mention it. Then again, I had wondered for quite some time how
I could make this happen but never quite got around to researching a
solution.
On the theory that others might be in the same position, I thought I'd post
this in case it helps anyone else who has been scratching their heads over
how to do the same thing. Maybe someone will benefit from this note, either
now, or in the future via the newsgroup archive.
By the way, although I've posted this to comp.lang.java.databases, this
technique will work with non-database code too; I just found that the need
for this technique seemed to happen more in database code than in regular
code.
Oh, what the heck, I'm going to crosspost this to comp.lang.java.programmer;
maybe someone there will benefit too
probable and improbable error condition that might occur?
For example, suppose you had this code and wanted to make sure that the
error handling did the right thing when an InstantiationException occurred:
----------------------------------------------------------------------------------------------------------------
/* Load the JDBC driver. */
try {
Class.forName(this.jdbcDriverName).newInstance();
}
catch (ClassNotFoundException cnf_excp) {
String msg = "Failed to load JDBC driver " + this.jdbcDriverName
+ ".";
this.logger.logp(Level.SEVERE, this.CLASS_NAME, METHOD_NAME,
msg, cnf_excp);
System.exit(16);
}
catch (InstantiationException i_excp) {
String msg = "Failed to load JDBC driver " + this.jdbcDriverName
+ ".";
this.logger.logp(Level.SEVERE, this.CLASS_NAME, METHOD_NAME,
msg, i_excp);
System.exit(16);
}
catch (IllegalAccessException ia_excp) {
String msg = "Failed to load JDBC driver " + this.jdbcDriverName
+ ".";
this.logger.logp(Level.SEVERE, this.CLASS_NAME, METHOD_NAME,
msg, ia_excp);
System.exit(16);
}
----------------------------------------------------------------------------------------------------------------
Well, up until a few minutes ago, I had always wondered if there was some
simple way to _force_ a desired exception, like an InstantiationException in
this case, to occur at the desired point so that I could make sure the
second catch would work.
Then, inspiration dawned. I tried adding 'throw new
InstantiantionException();' as the last statement in the try block, executed
the code, and BINGO, the InstantiationException occurred. My second catch
block was invoked and did what it was supposed to do.
Well, this is no great miracle by any stretch of the imagination and it
seems blindingly obvious as I mention it now; many of you are probably
yawning as I mention it. Then again, I had wondered for quite some time how
I could make this happen but never quite got around to researching a
solution.
On the theory that others might be in the same position, I thought I'd post
this in case it helps anyone else who has been scratching their heads over
how to do the same thing. Maybe someone will benefit from this note, either
now, or in the future via the newsgroup archive.
By the way, although I've posted this to comp.lang.java.databases, this
technique will work with non-database code too; I just found that the need
for this technique seemed to happen more in database code than in regular
code.
Oh, what the heck, I'm going to crosspost this to comp.lang.java.programmer;
maybe someone there will benefit too