Java异常处理

异常处理

什么是异常处理

  • 异常(Exception),或者“例外”
  • 软件出问题三个层次:(1)编译无法通过;(2)运行时异常;(3)逻辑错误
  • 案例:输入一个数字,计算该数字平方并显示

异常的危害

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

如何解决

?异常处理三个关键字

  • try:将有可能出现异常的代码放在try中
  • catch:将异常处理代码放在catch中
  • 原理:try中代码,如果不出现异常,则正常执行完毕,不执行catch内代码;如果出现异常,掠过剩余部分,直接执行catch内代码。
  • 要点:
    • try后面可以接多个catch,分别处理不同种类的异常
    • Java中常见的有哪些异常呢?
      -NumberFormatException:数值格式异常
      -NullPointerException:空引用异常
  • finally:如果一段代码,不管是否有异常都必须运行,那就放在finally中! finally能够确保try中不管出现什么情况,都会执行1次!
  • try(1)+catch(1+)+finally(0,1)
  • 异常处理有时候可以简化程序控制
    • 案例:输入一个数字,计算该数字平方并显示,如果用户输入格式有误,输入框不断重复显示,直到输入正确为止

异常自定义:

两个关键词

  • throws :定义该函数可能会抛出异常对象

  • throw : 在程序中抛出一个异常对象

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

  • throws和throw支持异常自定义并且在函数中链式传递
    -支持异常的就地捕获和异常的再次抛出

    例题:自定义非法年龄类IllegalAgeException,定义一个Person类,包含年龄,姓名,性别等属性,编写属性设置和读取函数,在设置年龄函数中,判断参数是否符合要求(1~150),如果不符合则抛出异常,编写main函数进行测试。

    package one;
    import java.util.*;
    class IllealException extends Exception
    {
    	String msg=new String();
    	IllealException(String msg)
    	{
    		this.msg=msg;
    	}
    	public String getMessage()
    	{
    		return msg;
    	}
    }
    
    
    public class Person {
    	private String name;
    	private String sex;
    	private int age;
    	Person()throws Exception
    	{
    		Scanner scanner=new Scanner(System.in);
    		System.out.print("请输入姓名:");
    		String name=scanner.next();
    		
    		System.out.print("请输入性别:");
    		String sex=scanner.next();
    		System.out.print("请输入年龄:");
    		int age=scanner.nextInt();
    		setName(name);
    		setSex(sex);
    		setAge(age);
    
    	}
    	void setName(String name) {
    		this.name=name;
    	}
    	void setSex(String sex) {
    		this.sex=sex;
    	}
    	String getName() {
    		return name;
    	}
    	String getSex()
    	{
    		return sex;
    	}
    	void setAge(int age)throws IllealException
    	{
    		if(age>150||age<1) 
    		{
    			throw new IllealException("年龄错误!");
    		}
    		this.age=age;
    	}
    	int getAge()
    	{
    		return age;
    	}
    	
    	public static void main(String[]a)throws Exception
    	{
    		new Person();
    	}
    	
    }
    
    
class Test{	
	public static void main (String[] args) {
		
		String str = javax.swing.JOptionPane.showInputDialog("请输入");
		double d = Double.parseDouble(str);
		System.out.println("AAAAAAAAAAAAAAAA");
		double result = d * d;
		System.out.println("平方是:" + result);
	
	}
}
2\
class Test{	
	public static void main (String[] args) {
		try{
			String str = javax.swing.JOptionPane.showInputDialog("请输入");
			double d = Double.parseDouble(str);			
			double result = d * d;
			System.out.println("平方是:" + result);
		}catch(Exception e){
			System.out.println("数值计算异常!");
		}	
		System.out.println("AAAAAAAAAAAA");
	}
}
3
class Test{	
	public static void main (String[] args) {
		try{
			String str = javax.swing.JOptionPane.showInputDialog("请输入");
			double d = Double.parseDouble(str);			
			double result = d * d;
			System.out.println("平方是:" + result);
		}catch(NumberFormatException e){
			System.out.println("数值格式异常!");
		}catch(NullPointerException e){
			System.out.println("错误点击撤销按钮");			
		}catch(Exception e){
			System.out.println("程序出现异常");			
		}
		
	}
}
4
class Test{	
	public static void main (String[] args) {
		try{
			String str = javax.swing.JOptionPane.showInputDialog("请输入");
			double d = Double.parseDouble(str);			
			double result = d * d;
			System.out.println("平方是:" + result);
			return;
		}catch(Exception e){
			System.out.println("数值计算异常!");
		}	
		finally{ System.out.println("AAAAAAAAAAAA");	}
	}
}
5
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,"输入错误");
		   	}			
		}
	}
}
6
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;
	}
	public static void main (String[] args) {
		Test t = new Test();
		try{			//异常的就地捕获
			int a = t.setAge(1000);
		}catch(AgeException ae) {  System.out.println(ae.getMessage()); }
	}
}
7
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;
	}
	public static void main (String[] args)  throws AgeException{//再次抛出
		Test t = new Test();
		int a = t.setAge(1000);
	}
}
8
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()); }
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值