⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11.txt

📁 电子工业出版社出版的《java2应用开发指南》配套光盘源代码
💻 TXT
字号:

例程11-1
//ExceptionDemo.java
class ExceptionDemo
{
	public static void main(String args[])
	{
		for(int n=0;n<4;n++)
		{
			try
			{
				switch(n)
				{
					case 0:
						char c = “123456”.charAt(100);
					break;
					case 1:
						int[] a = null;
						a[0] = 10;
					break;
					case 2:
						int[] b = new int[10];
						b[15] = 15;
					break;
					case 3:
						int nNull = 0;
						int c = 10/nNull;
					break;
				}
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
	}
}



例程11-2
// ExecutionOrder.java
public class ExecutionOrder
{
	public static void main(String args[])
	{
		System.out.println("1");
		try
		{
			System.out.println("2");
			if( true)
				//抛出用户定义的异常
				throw new InstanceException();
			System.out.println("3");
		}
		//捕获该异常
		catch(InstanceException e)
		{
			System.out.println("4");
		}
		System.out.println("5");
	}
}
class InstanceException extends Exception
{
	public InstanceException()
	{
		super();
	}
}



例程11-3
//Demo.java
import java.io.*;
class IOExceptionDemo
{
	public IOExceptionDemo()
	{
		try
		{
			int a = 0;
			int b = 10;
			int c = b/a;
		}
		catch(ArithmeticException e)
		{
			System.out.println("arithmetic exception");
		}
		finally
		{
			System.out.println("Entering finally statement. This must be executed");
		}
	}
}
public class Demo
{
	public static void main(String args[])
	{
		IOExceptionDemo aIOExceptionDemo = new IOExceptionDemo();
	}
}



例程11-4
//UserExceptionDemo.java
class UserExceptionDemo
{
	//抛出异常的方法
	void doSomeThing()
	{
		int a = 0;
		int b = 10;
		//产生异常的条件判断
		if(a !=0)
		{
			System.out.println(“Normal”);
		}
		else
		{
			//抛出异常
			throw new UserDefineException();
		}
	}
	public static void main(String args[])
	{
		//实例化
UserExceptionDemo aUserExceptionDemo = new UserExceptionDemo();
//调用类实例的方法
		aUserExceptionDemo.doSomeThing();
	}
}
//用户定义的异常,由Exception类派生
class UserDefineException extends ArithmeticException
{
	//Exception message
	System.out.println(“Exception occured”);
}



例程11-5
// ListOfNumbersDeclared.java
import java.io.*;
import java.util.Vector;

class ListOfNumbers 
{
    private Vector victor;
    private static final int size = 10;

	//构造方法
	public ListOfNumbers () 
	{
			//创建向量类
	        victor = new Vector(size);
			//向向量中增加元素
	        for (int i = 0; i < size; i++)
	            victor.addElement(new Integer(i));
	}
	//成员方法,抛出两种类型的异常
	public void writeList() throws IOException, ArrayIndexOutOfBoundsException
	{
		//文件IO处理
        PrintStream out = new PrintStream(
			      new FileOutputStream("OutFile.txt"));
        //取得向量元素
        for (int i = 0; i < size; i++)
            out.println("Value at: " + i + " = " + victor.elementAt(i));
		//关闭输出流
        out.close();
    }
}

public class ListOfNumbersDeclared 
{
	public static void main(String[] args) 
	{
		//实例化对象
		ListOfNumbers list = new ListOfNumbers();
		try{
			//调用方法
			list.writeList();
		}
		catch( Exception e)
		{
			//打印出异常的栈跟踪
			e.printStackTrace();
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -