alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ControlFlow.java)

This example Java source code file (ControlFlow.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

controlflow, exception, wrong

The ControlFlow.java Java example source code

/* /nodynamiccopyright/ */ // hard coded linenumbers in other tests - DO NOT CHANGE
/*
 * Debuggee which exercises various types of control flow
 */

class ControlFlow {
    boolean b = true;
    int n = 22;

    public static void main(String args[]) throws Exception {
        (new ControlFlow()).go();
    }

    void go() throws Exception {
        if (b) {
            System.out.println("if, no else");
        }

        if (b) {
            System.out.println("if branch");
        } else {
            throw new Exception("Wrong branch!?");
        }

        if (!b) {
            throw new Exception("Wrong branch!?");
        } else {
            System.out.println("else branch");
        }

        try {
            throw new Exception();
        } catch (Exception e) {
            System.out.println("caught exception");
        } finally {
            System.out.println("finally");
        }

        // This isn't control flow at the source level,  but it is at the bytecode level
        synchronized (this) {
            System.out.println("synchronized");
        }


        for (int i = 0; i < n; i++) {
            System.out.println("Loop iteration: " + (i+1) + "/" + n);
        }

        switch (n) {
            case 0:
                throw new Exception("Wrong branch!?");
            case 1:
                throw new Exception("Wrong branch!?");
            case 2:
                throw new Exception("Wrong branch!?");
            case 3:
                throw new Exception("Wrong branch!?");
            case 22:
                System.out.println("switch case");
                break;
            default:
                throw new Exception("Wrong branch!?");
        }

        switch (n) {
            case 0:
                throw new Exception("Wrong branch!?");
            case 1:
                throw new Exception("Wrong branch!?");
            case 2:
                throw new Exception("Wrong branch!?");
            case 3:
                throw new Exception("Wrong branch!?");
            default:
                System.out.println("switch default");
                break;
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java ControlFlow.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.