Thursday 16 June 2016

Java Tutorial : Java checked exception


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

Click the below Image to Enlarge
Java Tutorial : Java checked exception 
CheckedExceptionDemo1.java
import java.io.FileInputStream;

/*
 * Checked exceptions gets checked during compile time.
 * Since we didn’t handled/declared the exceptions, our
 * program gave the compilation error.
 */

public class CheckedExceptionDemo1
{

    public static void main(String[] args)
    {
        FileInputStream fis = null;

        /*
         * This constructor FileInputStream(File filename)
         * throws FileNotFoundException which is a checked
         * exception
         */
        fis = new FileInputStream("./myfile.txt");
        int i;

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

        /*
         * The method close() closes the file input stream
         * It throws IOException
         */
        fis.close();
    }

}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Unhandled exception type FileNotFoundException
    Unhandled exception type IOException
    Unhandled exception type IOException

    at CheckedExceptionDemo1.main(CheckedExceptionDemo1.java:21)
CheckedExceptionDemo2.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CheckedExceptionDemo2
{

    // Declare the exception using throws keyword.

    public static void main(String[] args)
            throws FileNotFoundException, IOException
    {
        FileInputStream fis = null;

        /*
         * This constructor FileInputStream(File filename)
         * throws FileNotFoundException which is a checked
         * exception
         */
        fis = new FileInputStream("./myfile.txt");
        int i;

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

        /*
         * The method close() closes the file input stream
         * It throws IOException
         */
        fis.close();

    }

}
Output
Hello
CheckedExceptionDemo3.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CheckedExceptionDemo3
{

    public static void main(String[] args)
    {

        FileInputStream fis = null;

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

            /*
             * This constructor FileInputStream(File
             * filename) throws FileNotFoundException which
             * is a checked exception
             */
            fis = new FileInputStream("./myfile.txt");
            int i;

            /*
             * Method read() of FileInputStream class also
             * throws a checked exception: IOException
             */
            while ((i = fis.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 file input
                 * stream It throws IOException
                 */
                if (fis != null)
                {
                    fis.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

    }
}
Output
Hello 
Inside finally block
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_CheckedException_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/510730a975724a569c9343782aaa0bffd03472e7/BasicJava/ExceptionDemo_CheckedException_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