异常 (exception): 发生在程序执行期间, 表明出现了一个非法运行的情况. 许多 JDK 中的方法在检测到非法情况时, 都会抛出一个异常对象. 例如: 数组越界和被 0 除.
代码验证:
- package test;
- import javax.swing.*;
- class AboutException {
- public static void main(String[] a)
- {
- int i=1, j=0, k;
- try
- {
- k = i/j; // Causes division-by-zero exception
- //throw new Exception("Hello.Exception!");
- }
- catch ( ArithmeticException e)
- {
- System.out.println("被 0 除."+ e.getMessage());
- }
- catch (Exception e)
- {
- if (e instanceof ArithmeticException)
- System.out.println("被 0 除");
- else
- {
- System.out.println(e.getMessage());
- }
- }
- finally
- {
- JOptionPane.showConfirmDialog(null,"OK");
- }
- }
- }
输出结果:
当 java 程序中出现多 try catch 的情况时, 一定要注意程序执行的先后顺序.
多 try catch 的 java 异常处理代码一
- package test;
- public class CatchWho {
- public static void main(String[] args) {
- try {
- try {
- throw new ArrayIndexOutOfBoundsException();
- }
- catch(ArrayIndexOutOfBoundsException e) {
- System.out.println( "ArrayIndexOutOfBoundsException" + "/ 内层 try-catch");
- }
- throw new ArithmeticException();
- }
- catch(ArithmeticException e) {
- System.out.println("发生 ArithmeticException");
- }
- catch(ArrayIndexOutOfBoundsException e) {
- System.out.println( "ArrayIndexOutOfBoundsException" + "/ 外层 try-catch");
- }
- }
- }
程序运行结果:
多 try catch 的 java 异常处理代码二
- package test;
- public class CatchWho2 {
- public static void main(String[] args) {
- try {
- try {
- throw new ArrayIndexOutOfBoundsException();
- }
- catch(ArithmeticException e) {
- System.out.println( "ArrayIndexOutOfBoundsException" + "/ 内层 try-catch");
- }
- throw new ArithmeticException();
- }
- catch(ArithmeticException e) {
- System.out.println("发生 ArithmeticException");
- }
- catch(ArrayIndexOutOfBoundsException e) {
- System.out.println( "ArrayIndexOutOfBoundsException" + "/ 外层 try-catch");
- }
- }
- }
程序运行结果:
当有多个 try catch finally 嵌套 时, 要特别注意 finally 的执行时机. 特别注意: 当有多层嵌套的 finally 时, 异常在不同的层次抛出, 在不同的位置抛出, 可能会导致不同的 finally 语句的执行顺序. 再者, 我们一般认为 finally 语句中的句子一定会被执行, 这里的一定是相对而言的, 并不是绝对的. 例如以下程序代码的运行:
- package test;
- public class SystemExitAndFinally {
- public static void main(String[] args)
- {
- try{
- System.out.println("in main");
- throw new Exception("Exception is thrown in main");
- //System.exit(0);
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- System.exit(0);
- }
- finally
- {
- System.out.println("in finally");
- }
- }
- }
运行结果:
在这段 java 代码中 finally 语句块并没有执行.
通过过异常的学习, 自己尝试了自定义了一个异常类来处理异常, 源码如下:
- package classtest;
- import java.util.Scanner;
- class Myexception extends Exception {
- public Myexception(String message) {
- super(message);
- }
- }
- public class Mytest {
- public static void main(String[] args) throws Exception{
- // TODO Auto-generated method stub
- try{
- function();
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- System.out.println("main() 函数运行时出现异常");
- }
- finally {
- System.out.println("main() 函数运行完毕");
- }
- }
- public static int judge(String str) {
- int c = 0;
- String regex = "[0-9]+";
- String regex1="[-][0-9]+";
- boolean d = str.matches(regex);
- boolean e = str.matches(regex1);
- if (d == false) {
- c = 1;
- if (e == true) {
- c = 0;
- }
- }
- return c;
- }
- public static void function() {
- try {
- System.out.println("请输入一个正整数:");
- Scanner input = new Scanner(System.in);
- String a = input.next();
- int temp = judge(a);
- if (temp == 1) {
- throw new Myexception("不能输入非法字符");
- } else {
- int num = Integer.parseInt(a);
- if (num < 0)
- throw new Myexception("输入不能为负数");
- else if (num == 0) {
- throw new Myexception("正整数不包括 0");
- }
- }
- } catch (Myexception f) {
- System.out.println(f.getMessage());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- System.out.println("function() 函数运行时出现异常");
- }
- finally {
- System.out.println("function() 函数已运行完毕, 请指示下一步动作");
- }
- }
- }
来源: https://www.cnblogs.com/weixiao1717/p/11780111.html