Hi, I am Martin.

This is my blog on mainly technology, entrepreneurship and design. Links about just about anything comes through now and then though. I also run a site on photography called Digital Photo Guide! If you are into photography, why not check that out?

Go to digitalphotoguide.net

Basic exception handling

WHEN WE EXECUTE a bit of code there is almost always the possibility for something to break. Sometimes it can be the purpose of the code to break because if it didn’t do so the consequences might be more severe. When these cases arise we call them exceptions or exceptional events.

To explain this in it’s most simple state we can look at a very small program which consist of a parent-class (main) and a child-class (subMeth) in which the child-class tries to divide an integer with zero.

public class TopDog {
  public static void main(String[] args) {
    SmallDog(5,0);
  }

  public static void SmallDog(int first, int second) {
    int average  = first/second;
  }
}

WHEN DIVISION BY ZERO is executed the engine will scream from the top of it’s lungs at the child telling him/her that division by zero is fobidden. Of course the child will run to it’s closest parent and start weeping. And due to the fact that parents usually are quite bad at math the problems can not be solved even here and this makes the entire world collapse. I.e the application close.

So, how do we stop the sky from falling? We build a way for the application to solve the problem when it arise so that the parent don’t need to receive the problem at all. To do this we use the keywords try and catch.

public class TopDog {
  public static void main(String[] args) {
    try {
      SmallDog(5,0);
    }
    catch (Exception e) {
      System.out.println(”Exception : “+ e.getMessage());
    }
  }

  public static void SmallDog(int first, int second) {
    int average  = first/second;
  }
}

This would make the same exception to be thrown from division by zero but we will catch the error and let the application continue while explaining for the user what happened. Output to use would give the same error as before but the application do not crash.

SOMETIMES WE WANT to execute a bit of code after a try regardless of an exception is thrown or not. It could be a file we tried to read from that we of course would like to close to not let memory and sockets linger. In Java this is possible by using the keyword finally. The finally keyword always come just after a try or catch. No extra code can be written in between.

public class TopDog {
  public static void main(String[] args) {
    try {
      SmallDog(5,0);
    }
    catch (Exception e) {
      System.out.println(”Exception : “+ e.getMessage());
    }
    finally {
      System.out.println("The end of the line.");
    }
  }

  public static void SmallDog(int first, int second) {
    int average  = first/second;
  }
}

In this example the string “The end of the line” would be printed regardless if an exception was thrown or not.

Simple class-design in Java

WHEN I TRY TO LEARN a new subject I always thought the best way is to try to explain what I’ve learned to someone else. Therefore I’m continue my experience with Java here and try to xplain in steps what I have been doing and learning.

So let’s get to create an easy class with some basic content. We start with the skeleton of my ‘Car’ class.

public class Car {
  public Car(){
  }
}

We need to state what fields exist for our car, in other words what are the caracteristics of our car we see on all cars that exist.

public class Car {
  private int speed;  // current speed of our car
  private int passangers; // the number of passangers

  public Car(){
  }
}

If we want the passangers-variable to always be set to four when we create a new car we just place it within our constructor. We can also get values from the creator at this stage, we use this to set the speed.

public class Car {
  private int speed;  // current speed of our car
  private int passangers; // the number of passangers

  public Car(int speed){
    this.speed = speed;
    this.passangers = 4;
  }
}

AS YOU CAN SEE we use the keyword this to use the fields available to us within the class. Also noteworthy is that in our constructor we chose to call the incoming parameter for speed eventhough our field is called speed. This is no problems and we just use our this keyword to separate the one from the other.

Our fields was set to private, this means that only the class itself have access to the values stored within. Due to this we must create methods for getting and setting the values in some way. We call these methods getters and setters.

public class Car {
  private int speed;  // current speed of our car
  private int passangers; // the number of passangers

  public Car(int speed){
    this.speed = speed;
    this.passangers = 4;
  }

  public int getSpeed() {
    return this.speed;
  }

  public int getPassengers() {
    return this.passengers;
  }

  public void setSpeed(int speed) {
    this.speed = speed;
  }

  public void setPassengers(int passengers) {
    this.passengers = passengers;
  }

}

We now have the backbone of our class with getters and setters in place.

Hello Java!

Output from a simple Hello World application in JavaSO I FINALLY got into the spirit of coding Java, I know I know I have been talking about taking this up since the last time I wrote a line back at the university but now the time has come. As one always do when facing a new language or one you haven’t touched in quite some time, I picked up an editor and wrote my Hello World!

FIRST OF ALL I had forgotten all about that main declaration but when I got that one if kind of created itself.

public class Hello {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

Now, isn’t that sweet?

NOW, my next focus is expand this little hello world with some packaging, external data from file and how about some nifty try and catch slapped all over it?