In the Beginning Java5 section of the site we were introduced to flow control when we looked at the
switch statements and the
? : tenary operator in the Conditional Statements lesson.
We continued our examination of flow control in the Loop Statements lesson where we looked at the different forms of the for and
In this section we finish our studies on flow control by studying exceptions and assertions. Firstly we look at Java exceptions where we make a thorough investigation of the exception hierarchy and how Java's exception architecture hangs together. With an overview of exceptions under our belt we look at handling the various forms of exception in our code. After this we look at declaring and throwing exceptions in our code, which we can deal with straight away or allow to propogate up the stack to be dealt with elsewhere. We finish of our investigation of exceptions by creating our own bespoke checked and unchecked exceptions for when needed.
With exceptions covered we turn our attention to the assertion mechanism available in Java. We learn appropriate uses of assertions and when not to use them. We also look at the runtime commands used to enable assertions, as and when required.
In this lesson we begin our studies of Java exceptions by looking at the Java exception hierarchy and the various classes within it. We then list the methods from the
Throwable class along with the
checked and runtime exceptions available within the
java.lang package, which is implicitly imported into all our programs.
Now we know what the Java exception hierarchy looks like and the classes involved its time to start handling exceptions which may occur within our code. In this lesson we focus on the
finally keywords and how we use them to handle exceptions in our code
In this lesson we learn how to use the
throw keyword which allows to throw exceptions from within our methods. We also investigate how to declare exceptions using the
In this lesson we learn how to by create our own bespoke checked and unchecked exceptions and how we use them in our code.
In this lesson we learn about the Assertion mechanism and how to write and then enable any assertion code within our programs. Assertions are a mechanism, mainly used in development, that allow us to check conditions
that we expect to be
true. We use the
assert keyword for this purpose and if the condition returns
true as expected then normal program flow continues. If, however, the condition
false an assertion error is thrown.