实例46 trycatch捕获异常的实例
public class CatchException_01 {
public static void main(String[] args) {
int array[] = { 0, 1, 2, 3, 4, 5, 6 };
try {
for (int i = 0; i < 10; i++) {
System.out.println("array[" + i + "]=" + array[i]);
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("array[]的长度是" + array.length + ",数组下标越界了");
}
System.out.println("运行完毕");
}
}
实例47 try-catch-finally捕获异常的实例
public class CatchException_02 {
public static void hasException() {
Object obj[] = new String[4];
for (int i = 0; i < obj.length; i++) {
try {
if (i % 2 == 1)
obj[i] = new Integer(i);// 此句会发生ArrayStoreException异常
else
obj[i] = new Integer(i) + "";
System.out.print(" " + obj[i] + "\n");
} catch (ArrayStoreException e) {
System.out.println("出现ArrayStoreException异常,数组内的元素类型与数组类型不匹配");
} finally {
System.out.println("这是finally块,是程序必须执行的部分\n");
}
}
System.out.println("方法执行结束");
}
public static void main(String argv[]) {
hasException();
System.out.println("程序正常结束");
}
}
实例48 trycatch嵌套捕获异常的实例
package Chapter04.process;
public class CatchException_03 {
public static void main(String[] args) {
int array[] = new int[5];
int n = 0;
for (int i = 0; i < array.length; i++) {
array[i] = i;
}
for (int i = 0; i < 10; i++) { // 该循环中可能会出现数组索引越界异常
try {
try {
n = array[i] / (i - 1); // 这里可能会出现除0异常
} catch (ArithmeticException e) {
System.out.println("0不能做被除数");
}
System.out.println(n);
} catch (IndexOutOfBoundsException e) {
System.out.println("数组索引越界");
}
}
}
}
实例49 throws声明异常的实例一
package Chapter04.process;
public class ThrowsException_01 {
public static void main(String args[]) {
try { // 由于ShowException会抛出异常,所以编译器会强制程序员在此捕获异常
ShowException();
} catch (Exception e) {
System.out.println("这是只在ShowException中声明\n" + "并在main方法中捕获的异常:\n"
+ e);
}
}
public static void ShowException() throws Exception {// 在这里声明要抛出的异常
Object x = new Integer(0);
System.out.println((String) x);
}
}
实例50 throws声明异常的实例二
package Chapter04.process;
public class ThrowsException_02 { // 声明异常
public void catchThowException(int str)
throws ArrayIndexOutOfBoundsException, ArithmeticException,
NullPointerException { // 声明catchThows方法的同时指出要可以出现的异常类型
System.out.print(str + " ");
if (str == 1) {
int a = 0;
int b = 6 / a;
} else if (str == 2) {
String s[] = new String[5];
s[0].toCharArray();
} else if (str == 3) {
int[] a = new int[5];
a[10] = 10;
} else {
System.out.println("没有发现异常,系统正常执行");
}
}
public static void main(String args[]) {
ThrowsException_02 te02 = new ThrowsException_02();
try {
te02.catchThowException(0);
} catch (Exception e) {
System.out.println("异常:" + e.getMessage()); // 捕获Exception异常,并打印出相应的异常信息
}
try {
te02.catchThowException(1);
} catch (Exception e) {
System.out.println("异常:" + e); // 捕获Exception异常,并打印出相应的异常信息
}
try {
te02.catchThowException(2);
} catch (Exception e) {
System.out.println("异常:" + e); // 捕获Exception异常,并打印出相应的异常信息
}
try {
te02.catchThowException(3);
} catch (Exception e) {
System.out.println("异常:" + e); // 捕获Exception异常,并打印出相应的异常信息
}
}
}
实例51 throw抛出异常实例一
package Chapter04.process;
public class ThrowException_01 {
public static void main(String argv[]) {
try {
// 由于ThrowException声明会抛出异常,所以编译器会强制程序员在此捕获异常
ThrowException();
} catch (Exception e) {
System.out
.println("这是由ThrowException方法抛出\n" + "在main方法中捕获的异常:" + e);
}
}
public static void ThrowException() {// 这里还是可以声明要抛出异常
double a = Math.random();
if (a < 0.5)
System.out.println(a);
else
throw new NumberFormatException();
}
}
实例52 throw抛出异常实例二
package Chapter04.process;
import java.util.Scanner;
public class ThrowException_02 {
public static void main(String[] args) {
showValue();
}
public static void showValue() {
System.out.println("请输入:");
Scanner sc = new Scanner(System.in); // 创建Scanner对象
String str = sc.next(); // 获取由键盘输入的内容
if (str.equals("close")) { // 判断是否输入内容等于close
sc.close(); // 如果是则关闭Scanner
throw new UnsupportedOperationException(); // 抛出UnsupportedOperationException
} else {
System.out.println("由键盘输入的内容为:" + str);
}
}
}
实例53 自定义异常一
package Chapter04.process;
public class CustomException_01 {
public static void main(String[] args) {// 捕获userException
try {
throw new userException(
"This is the definition of my own exception classes");
} catch (userException e) {// 打印userException异常信息
System.out.println("异常信息是:\n" + e.toString());
}
}
}
// 创建一个异常类
class userException extends Exception {
public userException(String message) {// 根据指定的异常信息自定义userException
super(message);
}
}
实例54 自定义异常二
package Chapter04.process;
public class CustomException_02 {
public static void main(String[] args) {
try {
customException();
formatThrowable();
} catch (CustomException e1) { // 捕获CustomException异常
System.out.println("Exception: " + e1.getMessage());
e1.printStackTrace(); // 输出异常信息
} catch (FormatThrowable e2) { // 捕获FormatThrowable异常
System.out.println("Exception: " + e2.getMessage());
e2.printStackTrace(); // 输出异常信息
}
}
public static void customException() throws CustomException {
Throwable cause = new Throwable("继承Exception类的CustomException()方法发生异常!");
throw new CustomException("发生错误!", cause);
}
public static void formatThrowable() throws FormatThrowable {
throw new FormatThrowable("继承Throwable类的throwException()方法发生异常!");
}
}
class CustomException extends Exception { // 该类继承父类Exception
public CustomException() { // 默认构造方法
super(); // 继承父类的的默认方法
}
public CustomException(String message) { // 含有指定详细消息的构造方法
super(message);
}
public CustomException(Throwable cause) { // 含有指定的原因的构造方法
super(cause);
}
public CustomException(String message, Throwable cause) { // 含有指定详细消息和指定的原因的构造方法
super(message, cause);
}
}
class FormatThrowable extends Throwable { // 该类继承Throwable类
public FormatThrowable() { // 默认构造方法
super(); // 继承父类的的默认方法
}
public FormatThrowable(String message) { // 含有指定详细消息的构造方法
super(message);
}
public FormatThrowable(Throwable cause) { // 含有指定的原因的构造方法
super(cause);
}
public FormatThrowable(String message, Throwable cause) { // 含有指定详细消息和指定的原因的构造方法
super(message, cause);
}
}