Let me give you some example to debug and track the flow.
Program-1
- ———–Illegal code
- package technical.jungle.com.exception;
- public class DemoArithmeticException {
- public static void main(String arfs[]) throws ArithmeticException {
- System.out.println(“www.TechnicalJungle.com”);
- System.out.println(“try”);
- int x = 0;
- int y = 10;
- int z = y / x; // exception. Developer has to catch this.
- System.out.println(“This code will never run”);
- System.out.println(“all done”);
- }
- }
Output:
Exception in thread “main” www.TechnicalJungle.com
try
java.lang.ArithmeticException: / by zero at technical.jungle.com.exception.DemoArithmeticException.main(DemoArithmeticException.java:11)
Look at the above example. Line number 12. Developer has to understand the situation and need to handle the exception by putting try{..} catch {..} clause.
- ———Legal Code
- package technical.jungle.com.exception;
- public class DemoArithmeticException {
- public static void main(String arfs[]) throws ArithmeticException {
- System.out.println(“www.TechnicalJungle.com”);
- try {
- System.out.println(“try”);
- int x = 0;
- int y = 10;
- int z = y / x;
- System.out.println(“This code will never run”);
- } catch (ArithmeticException e) {
- System.out.println(“catch”);
- }
- System.out.println(“all done”);
- }
- }
Output:
www.TechnicalJungle.com
try
catch
all done
Important points from above example
- Line 6 has exception declared.
- Line 8 to Line 16 has exception handle which is known for monitoring and taking appropriate action section.
- There are no need to declare the exception (by throws). But if you have added then its not a problem.
- It is possible then handle is some exception and declare is some other exception.
Program-2
Write a program to read characters from files and print it.
Precondition: “readFile.txt” files has been kept at d”// drive.
- —– Illegal Code
- package technical.jungle.com.exception;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- public class FileOperationTechnicalJungle {
- public static void main(String args[]) {
- FileReader reader = null;
- try {
- System.out.println(“www.TechnicalJungle.com”);
- File file = new File(“readfile.txt”);
- reader = new FileReader(file);
- char[] a = new char[60];
- reader.read(a); // reads the content to the array
- for (char c : a)
- System.out.print(c); // prints the characters one by one
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- reader.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Output:
java.io.FileNotFoundException: readfile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source) at technical.jungle.com.exception.FileOperationTechnicalJungle.main(FileOperationTechnicalJungle.java:12)
Exception in thread “main” java.lang.NullPointerException at technical.jungle.com.exception.FileOperationTechnicalJungle.main(FileOperationTechnicalJungle.java:21)
Important points from above example
- Created a txt file manually and kept at “D:” drive.
- Line 11 is not giving full proper path to get the file destination.
- So “file” object reference is null and Line 12 gives NullPointerException.
- In order to fix NullPointerException, have a look on the below code.
- package technical.jungle.com.exception;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- public class FileOperationTechnicalJungle {
- public static void main(String args[]) {
- FileReader reader = null;
- try {
- System.out.println(“www.TechnicalJungle.com”);
- File file = new File(“D://readfile.txt”);
- reader = new FileReader(file);
- char[] a = new char[60];
- reader.read(a); // reads the content to the array
- for (char c : a)
- System.out.print(c); // prints the characters one by one
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- reader.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Output:
www.TechnicalJungle.com
I am doing programming with www.TechnicalJungle.com
Important points from above example
- Line 12 has proper file path.
- Since Line 12 has proper path, so file object is referencing to proper memory location. So no NullPointerException at Line 13.
- Then after creating character array to store the characters from file.
- Check out the Line 23, finally block.
- Finally block has the code block to clean the resource or close the file.
If you like the post then please socialize it. Happy Sharing. Loudly Subscribing.