≡ Menu

Podcast Episode 14

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.


Here’s an example of a finally block:

FileInputStream stream = new FileInputStream(file);
  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);
catch (IOException e)

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.


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(

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!

{ 6 comments… add one }

  • Bernd February 4, 2013, 1:47 am

    Thanks again, Trevor.
    Those few lines of code raise a lot of questions:

    1. Why is the first line outside of the try block?
    2. Will the finally block be executed if that first line raises an exception?
    3. What happens if an exception occurs inside the catch block?
    4. What happens if an exception occurs inside the finally block? stream might be null or raise an exception in close()…
    5 What happens if you nest try/[catch/]finally statements? Will all finally blocks be executed and in which order?

    • Trevor Page February 5, 2013, 11:57 am

      Great Questions:

      1) The first line is outside the try block because it is referenced in the finally block… if it was just declared in the try block, then the scope of that variable would only stretch as far as the curly braces of the try block (i.e. you would get a compile error in the finally block saying that the stream variable was not declared).

      2) If by the “first line” you mean the initialization of the stream variable, then no, the finally block will not be executed. The program would likely just terminate with a nasty exception… unless the code (which imaginary at this point) that CALLS our file reading code here handles the Exception, because remember Exceptions propagate down the Stack, so if the calling method has a try/catch block, then it would be able to handle this Exception and the program would be able to keep on trucking.

      3) If there’s an Exception inside the catch block, then you’d either have to create yet another try/catch block inside the outer catch block (not recommended as it’s poor coding), or you just let the Exception happen and propagate down the call stack… In most everyday scenarios, there’s not much chance of an Exception being thrown in a catch block (unless you’ve got bad code).

      4) I would reiterate statement 3) above… you’d need a try/catch in your finally block, or you’d just let the Exception propagate… again, if you have exceptions in your finally block, this is generally caused by poor coding.

      5) finally blocks get executed when the code flows into it’s corresponding try block… so if the case was the your nested try/catch/finally blocks have code flowing into each try section, then yes both finally blocks would get executed. The order is still up in the air I believe, as it’s never guaranteed, but I tested it out and it looks like the inner finally is executed before the outer finally.

  • Javin Paul February 5, 2013, 11:35 pm

    Good one mate. I love your podcasts. By the way can you tell me more about your Intro music, I mean from where did you bought that? It’s awesome.

    • Trevor Page February 6, 2013, 8:21 am

      Good question Javin,

      I did a quick Google search and I found the site that actually has my intro song to buy. It’s from musicloops.com/. But I also hired John Melley to be the voice talent over the music track.

  • Dan Wood February 26, 2013, 9:40 pm

    Hey Trevor,

    Learning SO much from your podcasts. D0 y0u plan on covering multi-threading in detail?

Leave a Comment