Try Catching Explosions
For some reason it seems like a lot of people who are just starting their programming degrees claim that try-catch blocks are a hard concept. That combined with a little office humor inspired this post:
Okay, the basics: a try-catch block in Java is really composed of the following:
try{}
Catch(Exception e){}
finally{}
So we place any code we think might cause an exception in the try part and thecatch part is where we place code to handle the exception and the finally block executes if there's an exception thrown or not. So using a subclass of Exception, ExplosionException, we can check code for explosions and deal with them as follows:
methodThatCanNeverExplode();
try{
methodThatMightExplode();
}
catch(ExplosionException e){
methodToDealWithAnExplosionWhenItOccurs();
}
finally{
methodToExecuteAfterTheMethodThatMightExplodeRegardless();
}
Okay so maybe this is a useless post that does nothing more than make my blog more recently updated and let me ramble about a subject usually covered in an introductory programming course.
Okay, the basics: a try-catch block in Java is really composed of the following:
try{}
Catch(Exception e){}
finally{}
So we place any code we think might cause an exception in the try part and thecatch part is where we place code to handle the exception and the finally block executes if there's an exception thrown or not. So using a subclass of Exception, ExplosionException, we can check code for explosions and deal with them as follows:
methodThatCanNeverExplode();
try{
methodThatMightExplode();
}
catch(ExplosionException e){
methodToDealWithAnExplosionWhenItOccurs();
}
finally{
methodToExecuteAfterTheMethodThatMightExplodeRegardless();
}
Okay so maybe this is a useless post that does nothing more than make my blog more recently updated and let me ramble about a subject usually covered in an introductory programming course.
