Coding

This semester I am taking CompSci II, an advanced Java programming course at my school. So I figured I might as well live up to the name of my blog and talk about it. I am not planning on talking about CS I stuff- that is, basic programming- ifs, whiles, fors etc. unless I am asked. I’d also like you to keep in mind that I am still learning the CS II stuff, so if you see something that’s wrong or it doesn’t seem like I fully understand it, feel free to correct me in the comments.
It’s tough to kinda find a starting point for this- so I guess I can go by class. Tonight we did error handling and exceptions. There are 3 types of errors- syntax, logic, and run time. Exception handling is for run time errors, which, if the program is not coded correctly, will make the program crash. This takes away from the robustness of the program, where robustness is the property of reacting “gracefully” (as opposed to “crashing”) when abnormal conditions.
To prevent crashes, making the program more robust, we add exception handling in the form of a try/catch statement. a try/catch statement is set up like this:

try{
-code with possible bugs/errors/exceptions-
}
catch(excetionType1 name){
-how you want to handle the error-
}
catch(excetionType2 name){
-how you want to handle the error-
}
finally{
-statements executed after try and catch sections-
}

The way this is set up, you put code in the try section. If at any time in that code an error occurs (stated in Java as “An exception is thrown”) it will check the catch statements looking for that exception to see how to handle it. Usually, (in my experiences) the exception is simply handled with a print statement stating the error. In the future, I’m sure we will look into how to use exception handling not only to state the error, but also allow the program to keep going, or revert back to before the error. The finally statement will be executed whether or not an exception is thrown.
You can find a list of Java Exceptions here. Once my professor posts his notes on exception handling, I will post them here. Later

Update: Here is that link like I promised.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *