JAVA学习第六课:java异常处理

1、什么是异常处理
  • 异常(Exception),或者“例外”

程序运行过程中出现的不正常现象

  • 编译通不过,编译器自动识别,不属于异常
  • 编译通过,运行时程序不正常,属于异常
  • 编译通过,程序正常,结果正常:逻辑错误

这里主要讲解第二类:编译通过,程序运行时不正常

2、异常的危害
  • 向客户反馈不友好的信息
  • 程序发生异常,自动在异常处终止

例:输入一个数字,打印这个数字的平方

class Test{
	public static void main(String[] args){
		String str = javax.swing.JOptionPane.showInputDialog("输入数字");	
		int N = Integer.parseInt(str);
		int R = N*N;
		System.out.println("结果是:"+R);
	}
}

常见的异常类型有哪些?
1、ArithmeticException:算术异常,如除数为0
2、ArrayIndexOutOfBoundsException:数组越界
3、NullPointerException:未分配内存异常
4、NumberFormatException:数字格式异常

class Test{
	public static void main(String[] args){
		//int a = 1;int b = 0;int c = a/b;
		//int[] a =new int[5];
	}
}
3、如何解决?异常处理三个关键字
  • try:将有可能出现异常的代码放在try中
  • catch:将异常处理代码放在catch中
public class Test {
	public static void main(String[] args) {
		
		String str = javax.swing.JOptionPane.showInputDialog("输入数字");	
		try {
			int N = Integer.parseInt(str);
			int R = N*N;
			System.out.println("结果是:"+R);
		}catch(Exception e) {System.out.println("输入异常");}
		
	}

}
  • 原理:try中代码,如果不出现异常,则正常执行完毕,不执行catch内代码;如果出现异常,掠过剩余部分,直接执行catch内代码。
  • 要点:
    • try后面可以接多个catch,分别处理不同种类的异常

public class Test {
	public static void main(String[] args) {
		
		String str = javax.swing.JOptionPane.showInputDialog("输入数字");	
		try {
			int N = Integer.parseInt(str);
			int R = N*N;
			System.out.println("结果是:"+R);
		}catch(NumberFormatException e) {System.out.println("数字格式异常");}
		catch(Exception e) {System.out.println("输入异常");}
		
		
	}

}


public class Test {
	public static void main(String[] args) {
		try {

			System.out.println("访问数据库");
			System.out.println("处理数据");
			int a = 10/0;
		}
		catch(Exception e) {System.out.println("数据库链接异常");}
		finally {
			System.out.println("关闭数据库");
		}
		
	}

}

  • finally:如果一段代码,不管是否有异常都必须运行,那就放在finally中! finally能够确保try中不管出现什么情况,都会执行1次!
  • try(1)+catch(1+)+finally(0,1)
public class Test {
	public static void main(String[] args) {
		for(int i= 0;i <= 100;i++)
		{
			try {
				System.out.println("try");
				if(i==1) break;
			}catch(Exception e) {}
			finally {System.out.println("finally");}
		}
	}

}

class Test{	
	public static void main (String[] args) {
		while(true){
		   try{
			String str = javax.swing.JOptionPane.showInputDialog("请输入");
			double d = Double.parseDouble(str);			
			double result = d * d;
			System.out.println("平方是:" + result);		
			break;
		   }catch(Exception e){
		   		javax.swing.JOptionPane.showMessageDialog(null,"输入错误");
		   	}			
		}
	}
}
4、异常自定义:两个关键词
  • throws :定义该函数可能会抛出异常对象
  • throw : 在程序中抛出一个异常对象

案例:编写一个函数,setAge,输入一个年龄,如果年龄在0-100之间,返回年龄;否则返回“年龄错误”。

package customer;

public class Test {
	public int setAge(int age) throws Exception
	{
		if(age < 0||age > 100) 
		{
			Exception e = new Exception("年龄错误");
			throw e;
		}
		else 
		{
			System.out.println(age);
			return age;
		}
	}
	public static void main(String[] args) throws Exception {
		Test t = new Test();
		t.setAge(500);
	}
//	public static void main(String[] args){
//		Test t = new Test();
//		try {
//			t.setAge(500);
//		}catch(Exception e) {System.out.println(e.getMessage());}
//
//	}
}

  • throws和throw支持异常自定义并且在函数中链式传递
    -支持异常的就地捕获和异常的再次抛出
class AgeException extends Exception{
	String msg;
	AgeException(String msg){  this.msg = msg; }
	public String getMessage()			 {  return msg;	}
}
class Test{	
	int setAge(int age) throws AgeException{
		if(age>100||age<0){			
			throw new AgeException("年龄错误:" + age);			
		}
		return age;
	}
	void callSetAge() throws AgeException{
		int a = setAge(1000);
	}
	public static void main (String[] args)    {
		Test t = new Test();
		try{t.callSetAge();}
		catch(AgeException ae){   System.out.println(ae.getMessage()); }
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值