Black Book Series Blac Java TM Exception Handling Mahavir DS Rathore
Copyright Java TM 8 Exception Handling by Mahavir DS Rathore. While every precaution has been taken in the preparation of this book the author assume No responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. About the author I have has been programming & teaching Java for last 18 years. This book is an effort to document my knowledge to share with everyone across the world. I am available to do training on Java anywhere in the world. My email id is gurumahaveer@gmail.com.
Who should read the book? This book is for programmers who already know Core Java (Java Standard Edition) and interested to acquire better understanding of exception handling. Acknowledgement Java is owned by Oracle and trademark is acknowledged. Dedication To the inventors of the Java Language. Source Code For source code of this book please send me a mail at gurumahaveer@gmail.com. Table of Content
Sl.No | Chapter | HyperLink |
| Java Setup |
| What is Exception? |
| What is Exception Handling? |
| Java Exception Hierarchy |
| Try and Catch Block |
| Exception Handling API |
| Multiple Catch |
| Multiple Exceptions |
| Finally Block |
| Finally and return keyword |
| Checked and Unchecked Exception |
| Nested Exception |
| Throw Keyword |
| Exception Chaining |
| Try with Resource |
| Overriding Exception |
| User Defined Exception |
Chapter 1 : Software Setup Topics Introduction Software Required Verifying Installation Summary Introduction In this chapter I will show how to get and install the required software needed for learning exception handling using Java. There are many options available to have the required setup based upon operating system.
Java is free to download technology platform from oracle, many languages are supported on Java platform. Wikipedia has an exhaustive list of all JVM languages which can be seen by visiting https://en.wikipedia.org/wiki/List_of_JVM_languages . Software Required To develop Java programs you would need at the minimum a. Java SE Development Kit (Java 8) b. Code editor This is irrespective of the operating system. In this book I will be using Java on Windows OS.
Lets look at various software that are needed and from where we can get them.
- Java SE Development Kit
This is Java Standard Edition. Ensure you choose the correct JDK i.e. after identifying operating system and architecture (32 bit or 64 bit). The latest version is 8. It is very important to be cautious when downloading and installing Java, many programmers make a mistake of just downloading JRE and installing it.
JRE is the minimum environment required by a Java program to run whereas JDK is the minimum environment to compile and execute Java program, ensure you download JDK not JRE.
- Code editor I prefer to use Notepad++. Notepad++ is a great code editor, it has support for over 24 languages and it is open source.The latest version can be download from: https://notepad-plus-plus.org/download/ v6.8.8.html .You can also use other code editors or IDE such as eclipse, netbeans etc.
Verifying Installation On 32 bit Windows the Java SE is installed in C:\Program Files (x86)\Java by default. On 64 bit Windows it is installed in C:\Program Files\Java by default. To check if JDK is installed on your PC go to command line and run
- Java Compiler by typing Javac.exe
- Java Interpreted by typing java.exe
If for any of the above command you get a message is not recognized as an internal or external command then set the PATH environment variable using control panel. // Program 1 // Description: HelloWorld program class Program { public static void main(String args[]) { System.out.println("Helloworld - Java Exception Handling"); } } Summary To develop Java programs a programmer require JDK. // Program 1 // Description: HelloWorld program class Program { public static void main(String args[]) { System.out.println("Helloworld - Java Exception Handling"); } } Summary To develop Java programs a programmer require JDK.
JDK can be downloaded from oracle website. To code out Java programs programmer can use a code editor such notepad++. Chapter 2 What is Exception? Topics What is an Error? Cause of an Exception? Why to do Exception Handling? Summary What is an Error? Error is an unexpected situation within a program at compile or runtime. In a Java error can occur at compile time or runtime. Lets understand compile time error with the help of an example // Program 1 // Description: Compile time error class Program { public static void Main(String args[]) { // 'M' of Main is upper case - compiler error System.out.println("Namaskara Java") // There is no semi colon - compiler error } } When the above program is compiled, compiler will give compiler errors. This is first kind of error that Java gives.
The second kind of error is generated when the program is running. This kind of error is called an Exception. In other words Exception is an error that occur at runtime. When an Exception occur it will stop the program abruptly. This would be disastrous for mission critical applications such as Banking, Military, Ticketing etc. Hence increasing the reliability of an application is very important and that is why Exception handling is of highest importance.
Lets write a program that would stop (crash) abruptly. // Program 2 // Description : Runtime error i.e Exception class Program { public static void main(String args[]) { int i=0; int j=10/i; // yields in Arithmetic Exception } } In the above program when the integer is divided by zero program abruptly crashes. We dont want this to happen hence so we perform Exception handling. Lets look at another program which crashes abruptly. // Program 3 // Description: Runtime Exception Array out of bounds class Program { public static void main(String args[]) { args = new String[1]; args[0] ="Ram"; // ArrayOutOfBounds exception if no argument is passed System.out.println(args[0]); } } In the above program I show you another instance of an Exception. As we can infer that this program will also crash.
When faced with situation like this we perform exception handling. Causes of an Exception An Exception may occur in one of the three ways.
- The programmer can raise an exception using throw keyword. This is mainly done to raise user defined exception.
- Abnormal code execution yields into an exception. These exceptions are mainly predefined such as IOException, ArithmeticException, SocketException etc.
- When a program is out of memory or the JVM finds byte code is corrupt the application will certainly going to crash. These types of exceptions are called as Asynchronous exception.
Programmer cannot handle an asynchronous exception.
Why to Handle Exception? In a single sentence it can said software is very pervasive. Today software directly or indirectly impacts many domains such as banking, healthcare, business, military, education, government etc. Hence software reliability if of highest importance and exception handling help to enhance software reliability. There have been many examples of software crashing the business. Some of the the recent example are
- Tokyo stock exchange crashes
- Amazon Christmas Glitch
Next page