Wednesday 29 June 2016

Java Tutorial : Java Exception handling (Three kinds of Exceptions)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ffKwGbBeIVA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Exception handling (Three kinds of Exceptions) 
Java Tutorial : Java Exception handling (Three kinds of Exceptions) 
Java Tutorial : Java Exception handling (Three kinds of Exceptions) 
ExceptionDemo.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class ExceptionDemo
{

    public static void main(String[] args)
    {
        String fileName = null;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the fileName:");
        String value = scanner.next();
        if (value.length() > 5)
        {
            fileName = value;
        }

        scanner.close();

        FileReader fileReader = null;

        // Handle them using try-catch blocks.
        try
        {

            /*
             * This constructor FileReader(File filename)
             * throws FileNotFoundException which is a
             * checked exception
             */
            fileReader = new FileReader(fileName);
            int i;

            /*
             * Method read() of FileReader class also throws
             * a checked exception: IOException
             */
            while ((i = fileReader.read()) != -1)
            {
                System.out.print((char) i);
            }

        }
        catch (FileNotFoundException fileNotFoundException)
        {
            System.out.println("The specified file is not "
                    + "present at the given path.");
        }
        catch (IOException ioException)
        {
            System.out.println("I/O error occurred: " + ioException);
        }
        finally
        {
            try
            {
                System.out.println(" \nInside finally block");
                /*
                 * The method close() closes the
                 * fileReader,It throws IOException
                 */
                if (fileReader != null)
                {
                    fileReader.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }
}
Output
Enter the fileName:hello.txt
The specified file is not present at the given path.
 
Inside finally block

----------------------------------------------

Enter the fileName:a.txt
 
Inside finally block
Exception in thread "main" java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at ExceptionDemo.main(ExceptionDemo.java:34)
OutOfMemoryDemo.java
import java.util.HashMap;
import java.util.LinkedHashMap;


public class OutOfMemoryDemo
{

    public static void main(String[] args)
    {
        String str = "Hello";
        HashMap<String,String> map = new LinkedHashMap<String,String>();
        while(true)
        {
            str = str+"Welcome to india";
            map.put(str, str);
        }

    }

}
Output
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOfRange(Unknown Source)
        at java.lang.String.<init>(Unknown Source)
        at java.lang.StringBuilder.toString(Unknown Source)
        at OutOfMemoryDemo.main(OutOfMemoryDemo.java:14)
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_three_kinds_of_Exception_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_three_kinds_of_Exception_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4babce59a384cb4ab41f12b03cd7d11227a4b186/BasicJava/ExceptionDemo_three_kinds_of_Exception_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment