Java基础之Java异常

21 篇文章 0 订阅
21 篇文章 0 订阅

捕获错误最理想的是在编译期间,但有的错误只有在运行时才会发生。
对于这些错误,一般有两种解决方法:(哪一个更好)
遇到错误就终止程序的运行。
由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。

public class Demo1 {
	private int i;
	private Demo1 demo1;
	
	public Demo1() {
		demo1 = new Demo1();
	}
	
	public void test() {
		
	}
	public static void main(String[] args) {
		Demo1 d = new Demo1();
		System.out.println("iiii");
	}
}

异常

程序执行中发生的不正常情况。
开发过程中的语法错误不叫异常
Java的异常处理:处理非预期的情况,如文件没找到,空指针等。
Java程序运行过程中所发生的异常事件可分为两类:
Error: JVM系统内部错误、资源耗尽等严重情况//无法控制,不处理
Error(错误):是程序无法处理的错误,表示运行应用程序中较严重问题。

Exception: 其它因编程错误或偶然的外在因素导致的一般性问题。比如:空指针访问、试图读取不存在的文件等。
Exception(异常):是程序本身可以处理的异常
在这里插入图片描述

public class Demo2 {
	public static void main(String[] args) {
		TestClass t = new TestClass();
		t.test();
	}
}
class TestClass{
	public void test() {
		int[] num = new int[5];  // num[0] 1,2,3,4 
		
		System.out.println(num[num.length - 1]); // 运行时异常( );不需要处理。
		
		int a = 1;
		int b = 2;
		System.out.println(a + b);
		
		// 检测异常/ 非运行时异常 / 编译时异常    / 需要我们处理。
		File f = new File("c://hh.txt");
		try { 
			f.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}// try之后,并没有终止程序。接着执行。
		
		int x = 1;
		int y = 2;
		System.out.println(x + y);
		
	}
}

如何处理Exception异常

处理异常的方式:抓抛模型
1.抛:当执行程序时,一旦出现异常,就会生成一个对应异常类型的对象,并将对象抛出

  • ①.抛的类型:自动抛和手动抛
  • ②.此异常的对象将来是交给方法的调用者
    2.抓:抓住程序中抛出来的异常对象。
  • 异常处理的方式:
    1.try-catch-finally
  • try{
  • //可能会发生异常的代码
  • }catch(Exception e1){
    // 处理方式一
  • }catch(Exception e2){
    //处理方式二
  • }finally{
    // 一定要执行的代码
  • }
  • ①.try代码块里面的声明的变量,是局部变量,作用域是在代码块中
  • ②.finally是可选的,如果加了finally,不论是否出现异常,都会执行finally中代码
  • ③.catch语句是用来处理异常
    getMessage();printStrackTrace():打印异常信息(程序出错的位置信息及原因)
  • ④.catch代码块可以有多个,try中抛出的异常对象是从上往下去匹配catch中的异常类型,一旦满足就行
    catch中的代码。执行完毕,就跳出所有的catch。
    ⑤.如果catch中多个异常类型是并列关系,不需要考虑顺序问题。
    如果catch中多个异常类型有继承关系,子类需要在父类异常对象的上面。
  • ⑥.try-catch是可以嵌套多层。
  • 提示:
    1、对于运行时异常,可以不用显示的进行异常处理。
    2、对于编译时异常,必须要显示的进行异常处理。
public class Demo1 {
	public static void main(String[] args) {
		
		int[] num = new int[5];
		
		try {
			int x = 0;
			//System.out.println(num[5]);
		}catch(InputMismatchException e) {
			e.printStackTrace();
			System.out.println("捕获到异常。。。");
		}		// ArrayIndexOutOfBoundsException

		// 检测异常的处理
		File file = null;
		try {
			file = new File("xxxx");
			file.createNewFile();
			Class.forName("");
			Thread.sleep(124);
		}catch(IOException e) {
			e.printStackTrace();
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
		
		try {
			file = new File("xxxx");
			file.createNewFile();
			Class.forName("");
			Thread.sleep(124);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		
		System.out.println(file);
		int a = 1;
		System.out.println(a);
		
		test();
		//test1();
		test2();
	}
	// 类型转换异常  ClassCastExcetion
	public static void test1() {
		
		Object object = LocalDateTime.now();
		String str = "";
		str = (String)object;
	}
	// 空指针异常  NUllpointerExcetion
	public static void test2() {
		List s = getList();
		if(s != null) {
			System.out.println(s.size());		
		}
	}

	public static List getList() {
		//
		List l = new ArrayList<String>();
		l.add("a");
		
		return null;
	}
	
	
//	/ 2,算术异常  12/ 0 
	public static  void test() {
		
		int i = 12;
		try {
			System.out.println(i / 0);
		}catch (ArithmeticException e) {
		}
		System.out.println("sssssssssss");
	}
	
	// 除了RuntimeExpetion及子类之外的异常。都是编译异常,都需要通过try()或抛出的方式处理。
	public void test4() throws FileNotFoundException{
		FileInputStream fs = new FileInputStream("XXX");
	}
	
}

finally语句块使用

  • 注意: 有return的使用
public class Demo2 {
	public static void main(String[] args) {
		
		InputStream s = null;
		try {
			s = Demo2.class.getClassLoader().getResourceAsStream("jdbc");
			Properties p = new Properties();
			p.load(s);
			
		}catch(IOException e) {
			e.printStackTrace();
		}catch(Exception e) {
			
		} finally {
			try {
				s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
		
		
		System.out.println("end");
		
		test();
		
	}
	
	public static int test() {
		try {
			int s = 10 / 0;
			System.out.println("3=====");
			return 10;
		}catch (Exception e) {
			
		}finally {
			System.out.println("1=====");
		}
		System.out.println("2=====");

		return 0;
	}
	
}

在这里插入图片描述

throws语句的语法格式为

methodname throws Exception1,Exception2,…,ExceptionN
{
}
总结: try catch 抓 (自习处理)
throws 抓 (交给别人处理)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值