Java Programming Home Page: Archive: Message #110

Date: Oct 29 2000 10:33:50 EST
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: Exceptions and threads, example

Hi All,
Here is some sample code that shows how
to do both threads and exceptions....FYI.

Regards,
 - DL
public class Count implements Runnable {
    public static int n = 0;
    int tn = n;
    Count() {
    	tn = n;
    	n++;
    }
	public void run() {
		try {
		for (int i=0; i < 10; i++)
			System.out.print(i);
		System.out.println("thread#="+tn+" is am done");
		}
		catch (Exception e) {}
	}
}
public class MCP {
	Thread ta[] = new Thread[10000];
	public static void main(String args[]) {
		new MCP();
	}
	MCP() {
		try {
		for (int i=0 ; i < ta.length; i++) {
			ta[i]=new Thread(new Count());
			ta[i].start();
		}
		}
		catch(Exception e) {}
	}
}
public class Hello implements Runnable {
	Thread t = new Thread(this);
	Hello() {
		t.start();
	}
	public static void main(String args[]) {
		new Hello();
	}
	public void run() {
		System.out.println("hello world");
	}
}

public class CreditCardException extends Exception {
	CreditCardException(String s) {
		super(s);
	}
}
public class CheckCreditCard {
	public static void main(String args[]) {
		int i;
		for ( i=0;i<10;i++)
		  System.out.println(i);
		System.out.println("i="+i);
		CheckCreditCard ccc =
			new CheckCreditCard();
		try {
			ccc.creditCheck(10);
			try {
				double a = (1/0);
				System.out.println(a);
			}
			catch (ArithmeticException e) {
			    System.out.println("1/0 is not good...");
			}
		}
		catch (CreditCardException e) {
			e.printStackTrace();
			System.out.println("Heres the cc exception");
			return;
		}
		catch (ArithmeticException e) {
			System.out.println("are you dividing by zero again?");
			e.printStackTrace();
			double j = 1/0;
		}
		finally {
			System.out.println("clean up the mess!");
		}
		System.out.println("Bad things happened, so what?");
	}
	void creditCheck(int i) throws 
		CreditCardException {
		if (i < 0) {
			throw new CreditCardException(
				"bad credit card");
		}
	}
}