java异常处理

异常定义

异常(Exception) 是正常程序流程所不能处理或没有处理的异常情况或异常事件

异常类型

异常说明
ArithmeticException算术错误,如除以0
IllegalArgumentException方法收到非法参数
ArrayIndexOutOfBoundsException数组下标出界
NumberFormatException字符串转化为数字异常
ClassNotFoundException不能加载请求的类
IOExceptionI/O异常的根类
FileNotFouondException不能找到文件
EOFException文件结束
IllegalAccessException对类的访问被拒绝
NoSuchMethodException请求的方法不存在
InterruptedException线程中断

Exception类

Exception类是java.lang.Throwable类的一个子类
构造函数为:
public Exception();
public Exception(String s);
常用方法为:
public String toString();返回异常类信息

如何进行异常处理

  • java异常处理机制
    • 使用throws声明异常
    • 使用throw抛出异常
    • 使用try/catch处理异常,finally无论是否发生异常代码总能执行

try/catch块

public class Test {
	
	public static  void go(int a) throws Exception{
		if (a<10&&a>0) {
			System.out.println(a);
		}else{
			throw new Exception("数字大小不合法");
			 
		}
		
	}

	public static void main(String[] args) {
		
		try {
			go(12);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

try/catch/finally

public class ExceptionTest1 {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入除数:");
		int a=scanner.nextInt();
		try {
			int b;
			b=12/a;
		} catch (ArithmeticException e) {
			System.out.println("除数为零;");
		}finally {
			System.out.println("这一个部分不论有没有异常都可以实现");
		}
	}
}

这是一个比较好的例子
注意当为yes,为no的时候各自会输出那些语句从而更加深刻的理解异常
注意这里有自己定义的的异常类,所以必须要自己定义自己的异常类

public class TestException {
	static void doRisky(String test) throws ScaryException{
		System.out.println("start test");
		if ("yes".equals(test)) {
			throw new ScaryException();
			
		}
		System.out.println("end risk");
		return;
	}
	public static void main(String[] args) {
		String test="yes";
		try {
			System.out.println("start try");
			doRisky(test);
			System.out.println("end try");
		} catch (ScaryException se) {
			System.out.println("scary Exception");
		}finally {
			System.out.println("finally!");
		}
		System.out.println("end of main");
	}
	
}

多重catch块

因为异常也是类,所以这里也有多态相关还有子类异常父类异常
这里有很多的都与前面的多态很有引导性,还是比较复杂所以后面会继续补充,这里暂时空着

自定义异常类

//定义一个phone类里面的setprice方法限定值,不可以为负值和大于10000
//自定义两个异常的类
class NeException extends Exception{
    public NeException(){
    	System.out.println("数值不可以为负!");
    }
   public NeException(String message){
           super(message);
          System.out.println(getMessage());
   }
}

public class overException extends Exception{
	public  overException() {
		System.out.println("数值大于10000了");
	}
	public overException(String message) {
		super(message);
		System.out.println(getMessage());
	}
}

public class Phone {
	private long price;

	/**
	 * @param price
	 */
	public Phone() {
		
	}
	public Phone(long price) {
		super();
		this.price = price;
	}

	public long getPrice() {
		return price;
	}

	public void setPrice(int price) throws NeException,overException {
		if (price<0) {
					throw new NeException();
					//这里也可以直接调用自定义的含有String参数的构造函数
					//throw new NeException("不可以为负值");
				}
		else if (price>10000) {
			throw new overException();
		}
		else {
			this.price=price;
		}
			}
	

}
public class Test1 {
	public static void main(String[] args) {
		Phone phone= new Phone();
		try {
			phone.setPrice(-1);
		} catch (overException e) {
			e.getMessage();
		}catch (NeException e) {
			e.getMessage();
		}
	}

}

此文章还不完整后面会继续补充

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值