The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java Exception handling (Three kinds of Exceptions) Posted: Jun 29, 2016 8:10 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java Exception handling (Three kinds of Exceptions)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

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
  • Read: Java Tutorial : Java Exception handling (Three kinds of Exceptions)

    Topic: JGroups: Leader election without additional infrastructure Previous Topic   Next Topic Topic: The Librarian: Introduction to Test-Driven Development

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use