Book Image

Hands-On Automation Testing with Java for Beginners

Book Image

Hands-On Automation Testing with Java for Beginners

Overview of this book

Java is one of the most commonly-used software languages by programmers and developers. Are you from a non-technical background and looking to master Java for your automation needs? Then Hands-On Automation Testing with Java for Beginners is for you. This book provides you with efficient techniques to effectively handle Java-related automation projects. You will learn how to handle strings and their functions in Java. As you make your way through the book, you will get to grips with classes and objects, along with their uses. In the concluding chapters, you will learn about the importance of inheritance and exceptions with practical examples. By the end of this book, you will have gained comprehensive knowledge of Java.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Importance of the finally block in Java


There is one more block that is just like the try...catch block: is the finally block. The finally block will be executed irrespective of whether an exception is thrown. This block is executed if the program runs successfully, and even executed if the program doesn't run.

We'll explain this using the example we used in the The try...catch mechanism to handle exceptions section. We just add a finally block after the catch blocks and we give a print statement in it saying, delete cookies. The code block will look like this:

finally
{
    System.out.println("delete cookie")
}

When we run the code, we get the following output:

I caught the Arithmeticerror/exception
delete cookie

One important point is that finally can work with or without the catch block; all it needs is to be written below a try block.