vendredi 26 octobre 2018

Console output of Exception test often differs,

I'm running some sample Exception testing code, and I'm not completely sure why, however almost each time I run it I'm met with a different output.

The main method:

public class LocalExceptionTest {
private static void numberTest(int num) {
    boolean valid = false;

    try {
        if (num < 0 || num > 9) {
            throw new RangeException("out of range");
        }

        else
            valid = true;
    }
    // specialised exceptions must come first (otherwise the asbtract
    // exception will always get caught)
    catch (RangeException e) {
        System.err.println(e.getMessage());
    }
    // catch any other unexpected exception
    catch (Exception ex) {
        // nothing much we can do so just print a stack trace and continue
        ex.printStackTrace();
    } finally {
        System.out.println("always do this");
    }

    if (valid)
        System.out.println("valid data");
}

public static void main(String args[]) {
    numberTest(5);
    numberTest(12);
    numberTest(0);
    numberTest(-1);
}

And the exception:

{
   RangeException()
   {
      super();
   }

   RangeException(String message)
   {
      super(message);
   }
}

Here are just some of the outputs I am recieving, the "out of range" message comes in multple locations, however the rest seems to be fine.

enter image description here

enter image description here

Aucun commentaire:

Enregistrer un commentaire