Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Java异常处理Exception

$
0
0
常见:


算术异常类:ArithmeticExecption


空指针异常类:NullPointerException


类型强制转换异常:ClassCastException


数组负下标异常:NegativeArrayException


数组下标越界异常:ArrayIndexOutOfBoundsException


文件未找到异常:FileNotFoundException


方法未找到异常:NoSuchMethodException



自定义异常处理:


Exception1.java
public class Exception1 extends Exception{
	private int num;
	public Exception1(int a)
	{
		this.num=a;
	}
	public String toString(){
		return num+"的值必须大于10!";
	}
}


Exception2.java
public class Exception2 extends Exception{
	private int num;
	public Exception2(int a)
	{
		this.num=a;
	}
	public String toString(){
		return num+"的值必须小于100!";
	}
}

Test.java
public class Test
{
	public void makeException(int x) throws Exception1,Exception2
	{
		if(x<10)
		{
		throw new Exception1(x);
		}
		else if(x>100)
		{
		throw new Exception2(x);
		}
		else 
		System.out.println("ok");
	}
public static void main(String args[])
{
	Test t=new Test();
	try{
	t.makeException(118);
	}
	catch(Exception1 e)
	{
	System.out.println(e);
	}
	catch(Exception2 e)
	{
	System.out.println(e);
	}
}
}






作者:lhtisgood 发表于2013-4-2 20:46:27 原文链接
阅读:23 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>