8-1  HelloJava.java
public class HelloWorld {
	public static void main(String args[]) {
		int i = 0;
		String greetings[] = { Hello world!, No, I mean it!,
				HELLO WORLD!! };
		while (i  4) {
			System.out.println(greetings[i]);
			i++;
		}
	}
}
8-2  HelloWorld.java
public class HelloWorld {
	public static void main(String args[]) {
		int i = 0;
		String greetings[] = { Hello world!, No, I mean it!,
				HELLO WORLD!! };
		while (i  4) {
			try {
				System.out.println(greetings[i]);
				System.out.println(This is printed);
			} catch (ArrayIndexOutOfBoundsException e) {
				System.out.println(Re-setting Index Value);
				i = 10;
			}
			i++;
		}
	}
} 


8-3  HelloWorld.java
Handle an exception and move on.
import java.util.Random;

class HandleError {
	public static void main(String args[]) {
		int a = 0, b = 0, c = 0;
		Random r = new Random();
		for (int i = 0; i  32000; i++) {
			try {
				b = r.nextInt();
				c = r.nextInt();
				a = 12345  (b  c);
			} catch (ArithmeticException e) {
				System.out.println(Division by zero.);
				a = 0;  set a to zero and continue
			}
			System.out.println(a  + a);
		}
	}
}


8-4  MultiCatch.java
class MultiCatch {
	public static void main(String args[]) {
		try {
			int x = args.length;
			System.out.println(x =  + x);
			int y = 42  x;
			int z[] = { 1 };
			z[12] = 70;
		} catch (ArithmeticException e) {
			System.out.println(Divide by 0  + e);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println(Array index oob  + e);
		}
		System.out.println(After trycatch blocks.);
	}
}


8-5  FinallyDemo.java
Demonstrate finally.
class FinallyDemo {
	 Through an exception out of the method.
	static void procA() {
		try {
			System.out.println(inside procA);
			throw new RuntimeException(demo);
		} finally {
			System.out.println(procA's finally);
		}
	}

	 Return from within a try block.
	static void procB() {
		try {
			System.out.println(inside procB);
			return;
		} finally {
			System.out.println(procB's finally);
		}
	}

	static void procC() {
		try {
			System.out.println(inside procC);
		} finally {
			System.out.println(procC's finally);
		}
	}

	public static void main(String args[]) {
		try {
			procA();
		} catch (Exception e) {
			System.out.println(Exception caught);
		}
		procB();
		procC();
	}
}


8-6  ServerTimedOutException.java
public class ServerTimedOutException extends Exception {
	private String reason;
	private int port;

	public ServerTimedOutException(String reason, int port) {
		this.reason = reason;
		this.port = port;
	}

	public String getReason() {
		return reason;
	}

	public int getPort() {
		return port;
	}
}

8-7  ThrowDoc.java
Demonstrate throw.
class ThrowDoc {
	static void demoproc() {
		try {
			throw new NullPointerException(doc);
		} catch (NullPointerException e) {
			System.out.println(Caught inside demoproc.);
			throw e;  rethrow the exception
		}
	}

	public static void main(String args[]) {
		try {
			demoproc();
		} catch (NullPointerException e) {
			System.out.println(Recaught  + e);
		}
	}
}


8-8  ThrowsDoc1.java
public class ThrowsDoc1 {
	ThrowsDoc1() {
	}

	public void div(int x, int y) throws NullPointerException {
		try {
			int z = x  y;
			System.out.println(z);
			throw new NullPointerException(Doc);
		} catch (ArithmeticException e) {
			System.out.println(by0 + e);
		}
	}

	public static void main(String args[]) {
		ThrowsDoc1 docs = new ThrowsDoc1();
		try {
			docs.div(10, 2);
		} catch (NullPointerException e) {
			System.out.println(showthrows + e);
		}
		System.out.println(go on);
	}
}


8-9  GradeNew.java  
 0100ɼ򷴸ʾû룬ֱûȷ0100֮ĳɼٽеȼжҵûСַʱ쳣
import javax.swing.JOptionPane;

class GradeNew {
	public static void main(String[] args) {

		int score = 0;
		String grade = ;

String strScore = JOptionPane.showInputDialog(ķǶ٣);

		while (true) {
			try {
				score = Integer.parseInt(strScore);

				if (score  0  score  100) {
strScore = JOptionPane
	.showInputDialog(Ĳ0~100ٷƳɼ룺);
				}

				else
					break;
			} catch (NumberFormatException nfe) {
	strScore = JOptionPane.showInputDialog(null, һɼ);
			}
		}
		if (score  60) {
			grade = ;
	JOptionPane.showMessageDialog(null, ɼĵȼΪ   + grade
	+ nҪŬѽ, жɼȼ, JOptionPane.WARNING_MESSAGE);

	} else if (score  85) { ˴else̺ score=60 
			grade = ;
	JOptionPane.showMessageDialog(null, ɼĵȼΪ   + grade
+ n´ȡɣ, жɼȼ, JOptionPane.INFORMATION_MESSAGE);

		} else { ǰ2ų˵ score  85
			grade = ;
JOptionPane.showMessageDialog(null, ɼĵȼΪ   + grade + nϲ,жɼȼ, JOptionPane.INFORMATION_MESSAGE);

		}
	}
}

8-10  TestDBMException.java
дJava Applicationʵ¹ܣûͨԻȺ2doubleݣͳϢԻ
Ҫ󣺳ΪΪҪԶһ쳣DevideByMinusExceptionΪ쳣 Ϊʱʾû룬ǿǲ NumberFormatException
import javax.swing.JOptionPane;

public class DivideByMinusException extends Exception {
	private String excMsg;	
	public DivideByMinusException() {
		excMsg = Ϊ;
	}

	public String getMsg() {
		return excMsg;
	}
}

class TestDBMException {
	public static void main(String[] args) {
		double m;
		double n;
		String strM = ;
		String strN = ;
		strM = JOptionPane.showInputDialog();

		while (true) {

			try {
				m = Double.parseDouble(strM);
				break;
			} catch (NumberFormatException nfe) {
	strM = JOptionPane.showInputDialog(ݸʽ뱻);

			}
		}

		strN = JOptionPane.showInputDialog((Ǹ0));
		
		while (true) {

			try {
				n = Double.parseDouble(strN);
				if (n = 0)
					throw new DivideByMinusException();
				break;
			} catch (NumberFormatException nfe) {		
	strN = JOptionPane.showInputDialog(ݸʽ);
				
			} catch (DivideByMinusException de) {
strN = JOptionPane.showInputDialog(ΪΪ0);

			}
		}
 JOptionPane.showMessageDialog(null, m +    + n +  =  + m  n,
				, JOptionPane.INFORMATION_MESSAGE);
	}
}


8-11  TestArrayLength.java
һ쳣ArrayLengthExceptionڱʾ鳤ȵ쳣鳤=0100ʱ׳쳣
TestArrayLengthԸ쳣áڸmainһ int 飬鳤ûֵûĳֵ=0   100 ʱ׳ ArrayLengthException 
import javax.swing.JOptionPane;

class ArrayLengthException extends Exception {

	public ArrayLengthException(String Message) {
		super(Message);
	}
}

public class TestArrayLength {

	public static void main(String[] args) {

		int[] a;
		int len = 0;
		String strL = JOptionPane.showInputDialog(鳤ȣ);

		while (true) {

			try {
				len = Integer.parseInt(strL);
				if (len = 0)
			throw new ArrayLengthException(鳤=0쳣);

				if (len  100)
			throw new ArrayLengthException(鳤100쳣);
				break;

			} catch (NumberFormatException nfe) {
	strL = JOptionPane.showInputDialog(ִʽ룺);

			} catch (ArrayLengthException ale) {
JOptionPane.showMessageDialog(null, ale.getMessage()+  ֱӳʼ鳤Ϊ10);
				len = 10;
				break;
			}
		}

		a = new int[len];
	JOptionPane.showMessageDialog(null, ĳΪ + a.length);
	}
}










