zoukankan      html  css  js  c++  java
  • Java基础教程——异常处理详解

    异常处理

    好程序的特性

    • 可重用性
    • 可维护性
    • 可扩展性
    • 鲁棒性
      |--|--Robust的音译
      |--|--健壮、强壮之意
      |--|--指在异常和危险情况下系统依然能运行,不崩溃


    Java中,写下如下代码:

    public class TestException {
    	public static void main(String[] args) {
    		int a = 10 / 0;
    		System.out.println("-END-");
    	}
    }
    

    代码运行到“10 / 0”时出错,整个程序会中断,表现为最后的"-END-"没有输出。

     java.lang.ArithmeticException: / by zero
    

    很显然这不算好的程序,好的程序应该做好异常处理,不能说一处出错整体崩盘。


    异常处理

    异常处理,就是当程序出了错误时,系统不崩溃。

    顶层类:Throwable

    大分类 小分类
    Error 处理不了
    Exeption 编译时异常 必须处理(不处理编译不通过)
    运行时异常 可以处理

    Error示例:

    public class TestError {
    	// Exception in thread "main" java.lang.StackOverflowError
    	public static void main(String[] args) {
    		m();
    	}
    	static void m() {
    		m();
    	}
    }
    

    不断递归,导致出现栈溢出:

    Exception in thread "main" java.lang.StackOverflowError
    

    编译时异常:

    import java.io.FileNotFoundException;
    public class 异常处理 {
    	static void TestException1() {
    		try {
    			throw new FileNotFoundException("编译时异常,必须处理,要么自己处理,要么抛出");
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    	static void TestException2() throws FileNotFoundException {
    		throw new FileNotFoundException("编译时异常,必须处理,要么自己处理,要么抛出");
    	}
    	public static void main(String[] args) {
    		TestException1();
    		try {
    			TestException2();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    try.catch.finally

    Try{尝试执行}
    Catch{出错时执行}
    Finally{出不出错都执行,在return之前执行}
    
    public class 异常处理 {
    	public static void main(String[] args) {
    		int a = 10, b = 0;
    		TestException(a, b);
    	}
    	static boolean TestException(int a, int b) {
    		try {
    			System.out.println(a / b);
    		} catch (ArithmeticException e) {// 声明,此处可能抛出"算术异常"
    			System.out.println("系统维护中,请稍后重试");
    			return false;
    		} finally {
    			System.out.println("---END---");
    		}
    		return true;
    	}
    }
    

    运行结果:

    系统维护中,请稍后重试
    ---END---
    

    try(with resources)语法

    从Java 7开始,Java支持try-with-resources 语句,称为 ARM 块(Automatic Resource Management) ,自动资源管理——try...catch代码运行完毕后,自动释放资源,即使出现异常也会关闭资源。
    这种语句可以使得try...catch代码变得简洁(手动关闭资源往往需要考虑多种场景,导致代码臃肿)。

    这个功能在IO中用的比较多,Scnner没必要做异常处理,以下仅是一个示例:

    import java.util.Scanner;
    public class TryWith {
    	public static void main(String[] args) {
    		testTryWith();
    		testTryWith();
    	}
    	private static void testTryWith() {
    		try (Scanner sc = new Scanner(System.in)) {
    			sc.nextLine();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			System.out.println("finally");
    		}
    	}
    }
    
    finally
    java.util.NoSuchElementException: No line found
    finally
    
    扩展阅读:
    [try...catch的前世今生]: https://www.cnblogs.com/tigerlion/p/10659675.html
    

    throw自定义异常

    自定义异常类,继承Excption类

    一般需要写一个带参构造方法

    public class Test自定义异常 {
    	public static void main(String[] args) {
    		int a = 10;
    		TestException(a);
    	}
    	static boolean TestException(int money) {
    		try {
    			if (money < 10000) {
    				// 拋一個異常對象
    				throw new DidiException("系統升級,請半年后重試");
    			}
    		} catch (Exception e) {// 声明,此处可能抛出"算术异常"
    			e.printStackTrace();
    			return false;
    		} finally {
    			System.out.println("---END---");
    		}
    		return true;
    	}
    }
    class DidiException extends Exception {
    	public DidiException(String message) {
    		super(message);
    	}
    }
    

    运行结果:

    DidiException: 系統升級,請半年后重試
    ---END---
    	at Test自定义异常.TestException(Test自定义异常.java:10)
    	at Test自定义异常.main(Test自定义异常.java:4)
    
  • 相关阅读:
    1、听说过Redis吗?它是什么?
    55、数据库高并发是我们经常会遇到的,你有什么好的解决方案吗?
    54、数据库如何保证持久性?
    53、数据库如何保证原子性?
    52、数据库如何保证一致性?
    注解定义、基本语法和属性
    Macbook 装机必备--开发篇
    http
    python:beaufiful
    python-yield
  • 原文地址:https://www.cnblogs.com/tigerlion/p/11179218.html
Copyright © 2011-2022 走看看