Exception Handling Programming

Let me give you some example to debug and track the flow. 
Program-1
  1. ———–Illegal code
  2. package technical.jungle.com.exception;
  3. public class DemoArithmeticException {
  4. public static void main(String arfs[]) throws ArithmeticException {
  5. System.out.println(“www.TechnicalJungle.com”);
  6. System.out.println(“try”);
  7. int x = 0;
  8. int y = 10;
  9. int z = y / x;    // exception. Developer has to catch this. 
  10. System.out.println(“This code will never run”);
  11. System.out.println(“all done”);
  12. }
  13. }
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.
  1. ———Legal Code
  2. package technical.jungle.com.exception;
  3. public class DemoArithmeticException {
  4. public static void main(String arfs[]) throws ArithmeticException {
  5. System.out.println(“www.TechnicalJungle.com”);
  6. try {
  7. System.out.println(“try”);
  8. int x = 0;
  9. int y = 10;
  10. int z = y / x;
  11. System.out.println(“This code will never run”);
  12. } catch (ArithmeticException e) {
  13. System.out.println(“catch”);
  14. }
  15. System.out.println(“all done”);
  16. }
  17. }
Output:

www.TechnicalJungle.com
try
catch
all done
Important points from above example

  1. Line 6 has exception declared.
  2. Line 8 to Line 16 has exception handle which is known for monitoring and taking appropriate action section.
  3. There are no need to declare the exception (by throws). But if you have added then its not a problem. 
  4. 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.

  1. —– Illegal Code
  2. package technical.jungle.com.exception;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. public class FileOperationTechnicalJungle {
  7. public static void main(String args[]) {
  8. FileReader reader = null;
  9. try {
  10. System.out.println(“www.TechnicalJungle.com”);
  11. File file = new File(“readfile.txt”);
  12. reader = new FileReader(file);
  13. char[] a = new char[60];
  14. reader.read(a); // reads the content to the array
  15. for (char c : a)
  16. System.out.print(c); // prints the characters one by one
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. try {
  21. reader.close();
  22. } catch (IOException ex) {
  23. ex.printStackTrace();
  24. }
  25. }
  26. }
  27. }
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

  1. Created a txt file manually and kept at “D:” drive.
  2. Line 11 is not giving full proper path to get the file destination. 
  3. So “file” object reference is null and Line 12 gives NullPointerException.
  4. In order to fix NullPointerException, have a look on the below code.
  1. package technical.jungle.com.exception;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. public class FileOperationTechnicalJungle {
  6. public static void main(String args[]) {
  7. FileReader reader = null;
  8. try {
  9. System.out.println(“www.TechnicalJungle.com”);
  10. File file = new File(“D://readfile.txt”);
  11. reader = new FileReader(file);
  12. char[] a = new char[60];
  13. reader.read(a); // reads the content to the array
  14. for (char c : a)
  15. System.out.print(c); // prints the characters one by one
  16. }
  17. catch (IOException e) {
  18. e.printStackTrace();
  19. finally {
  20. try {
  21. reader.close();
  22. } catch (IOException ex) {
  23. ex.printStackTrace();
  24. }
  25. }
  26. }
  27. }  
Output:
www.TechnicalJungle.com
I am doing programming with www.TechnicalJungle.com



Important points from above example

  1. Line 12 has proper file path.
  2. Since Line 12 has proper path, so file object is referencing to proper memory location. So no NullPointerException at Line 13.
  3. Then after creating character array to store the characters from file.
  4. Check out the Line 23, finally block.
  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.