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 |
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(); } }
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)
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(); } }
Hello
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(); } } } }
Hello
Inside finally block
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:
No comments:
Post a Comment