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

Different kinds of exception


In this section, we will take a look at how we can handle exceptions in Java.

In general, if there is an error in the code, we need to catch it and print a message without failing; this can be done using the try...catch mechanism. So in general, when we try to write code and we suspect that there might be an error in it, we will use that error for exception handling.

We'll explain it with the help of an exercise. Let's create a new class, exceptionDemo, and inside the main block we declare the a, b, and c variables and assign values of 4, 7, and 0, respectively, to them. We add a try block inside the main block and we declare an integer variable, k, which is equal to b divided by c. Whenever we add anything in the try block, we are trying to see whether the code works. If it fails, the controller will come out of this try block and enter into the catch block that contains the exception. An important point to remember is that the catch block comes right after the...