My Next eBook

Hey Guys/Gals,

Before I jump into the show notes, I need your help… I'm getting ready to start writing my next book. I would absolutely LOVE IT if you could tell me what topic you would like to read about. I've got a bunch of knowledge in my head, and I really want to get it out into a book, but that whole process takes a while, so I want to make sure I make as many people happy as I possibly can.

So please Click Here to take a survey and let me know what you'd love to read about!

Trevor Page Featured Interviewed on Two Podcasts

smartpassiveincome.com/session55 – Pat Flynn is a giant in the entrepreneurial arena, and I'm thrilled to have been a small part of his story.

entrepreneuronfire.com/TrevorPage – John Dumas is a new contender in the entrepreneurial arena, but has been consistently hammering out some awesome daily interviews with today's most successful entrepreneurs.

Back to the Java!

Today you'll learn one more important aspect of Exceptions in Java. Also you'll learn what a Stack is and how a StackTrace is related to it.

finally

Here's an example of a finally block:

FileInputStream stream = new FileInputStream(file);
try
{
  Reader reader = new BufferedReader(new InputStreamReader(stream, cs));
  StringBuilder builder = new StringBuilder();
  char[] buffer = new char[8192];
  int read;
  while ((read = reader.read(buffer, 0, buffer.length)) > 0)
  {
    builder.append(buffer, 0, read);
  }
  System.out.println(builder.toString());
}
catch (IOException e)
{
  e.printStackTrace();
}
finally
{
  stream.close();
}

This code reads a file and outputs the contents into the console. The most important thing to note here is the finally block that closes the FileInputStream. If the stream.close() code was not in a finally block, and there was some sort of unexpected error (outside of an IOException) then the FileInputStream will not be closed, and it will potentially cause problems.

StackTrace

Here's an example of a typical StackTrace that you would encounter during your programming adventures:

Exception in thread “main” java.lang.Exception
    at net.javavideotutorials.example.TestProgram.openFile(TestProgram.java:12)
    at net.javavideotutorials.example.TestProgram.main(
TestProgram.java:7)

As you can see, each line holds a reference to a Class name, a method name and a line number… perfect for tracing the error back to its roots!

Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.