next up previous
Up: Unit 10 Previous: Example of exception handling

Exercises

Exercise 10.1 Determine whether the following program will generate (i) compilation errors, (ii) runtime errors. If the program does not generate errors, say what it will print out; if the program generates errors, correct them and say what it will print out after the correction. Motivate your answers.

public class Exercise1 {
  public static void main(String[] args) {
    for (int i = 0, j = 0; i < 10, j < 10; i++, j++) {
      System.out.println(i + " + " + j + " = " + (i+j));
    }
    System.out.println("I've printed out the sums of i and j up to "
                       + i + "," + j);
  }
}

Exercise 10.2 Determine whether the following program will generate (i) compilation errors, (ii) runtime errors. If the program does not generate errors, say what it will print out; if the program generates errors, correct them and say what it will print out after the correction. Motivate your answers.

public class Exercise2 {

  private int x = 101;

  private void f(int x) {
    x++;
    g();
  }

  private void g() {
    System.out.println(x);
  }

  public static void main(String[] args) {
    Exercise2 e = new Exercise2();
    int x = 200;
    e.f(x);
  }
}

Exercise 10.3 Determine whether the following classes will generate (i) compilation errors, (ii) runtime errors. If the program does not generate errors, say what it will print out; if the program generates errors, correct them and say what it will print out after the correction. Motivate your answers.

public class Base {
  public Base() {
    infob = "I am an object of the Base class";
  }
  public String getInfo() {
    return infob;
  }
  private String infob;
}

public class Derived extends Base {
  public Derived() {
    super();
    infod = "I am an object of the Derived class";
  }
  public String getInfo() {
    return infod + ", " + super.getInfo();
  }
  private String infod;
}

public class Exercise3 {
  public static void main(String[] args) {
    Base b = new Base();
    Derived d = new Derived();
    System.out.println(b.getInfo());
    System.out.println(d.getInfo());
    b = d;
    System.out.println(b.getInfo());
  }
}

Exercise 10.4 Capture all exceptions in the following program, printing out error messages that describe the type of error that occurred.

import java.io.*;

public class Exercise4 {

  public static void main(String[] args) {
    int n=10;
    int[] v = new int[n];
    FileReader f = new FileReader("dati.txt");
    BufferedReader in = new BufferedReader(f);
    int i=0;
    String linea = in.readLine();
    while (linea!=null) {
      v[i] = Integer.parseInt(linea);
      linea = in.readLine();
      i++;
    }
    f.close();
  }
}

Exercise 10.5 Solve Exercise 9.6 by handling explicitly all exceptions by printing out suitable error messages.

Exercise 10.6 Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads all lines of a file and throws an exception of type ExceptionLineTooLong in the case where a string of the file is longer than 80 characters. Handle also all exceptions that could be thrown by the program.

Exercise 10.7 Write a class containing the following static methods:

Solve the exercise by suitably catching exceptions.

Exercise 10.8 Define the exceptions that are necessary to catch the possible errors that can occur in the class Matrix of Exercise 9.9.

Modify the class Matrix in such a way that it generates the new exceptions when necessary.


next up previous
Up: Unit 10 Previous: Example of exception handling