您的位置:首页 > 其它

异常Exception相关

2016-04-15 00:00 357 查看
/**
* java异常处理中的try-catch
* @author Administrator
*
*/
public class ExceptionTest1{
public static void main(String[] args) {
System.out.println("程序开始了!");
try{
String str = null;
/*
* 当JVM运行过程中出现异常,都会实例化一个
* 该异常实例并抛出,然后检查该异常是否可以
* 被捕获,若没有被try包围,则会将该异常抛到
* 方法之外.
*/
System.out.println(str.length());
/*
* 当try中代码出现异常时,catch可以捕获抛出的异常
* 并得以解决,前提是catch捕获的异常是try中抛出的
* 异常类型.
*/
}catch(NullPointerException e){
//书写解决该异常的逻辑
System.out.println("出现了空指针!");
}
System.out.println("程序结束了!");
}
}
/**
* try不能独立使用,后面需要配合catch或finally
* 对于catch而言,可以有多个来分别捕获try中出现
* 的不同异常来进行处理.
* @author Administrator
*
*/
public class ExceptionTest2{
public static void main(String[] args) {
System.out.println("程序开始了");
try{

String str = "a";
System.out.println(str.length());
System.out.println(str.charAt(0));
System.out.println(Integer.parseInt(str));

}catch(NullPointerException e){
System.out.println("出现了空指针");
}catch(StringIndexOutOfBoundsException e){
System.out.println("字符串下标越界了!");

/*
* 应当养成一个习惯,在最下面捕获Exception
* 这样做可以防止出现没有捕获的异常而导致程序
* 中断.
*
* 若捕获的异常存在父子继承关系,那么子类异常
* 必须在上面先行捕获.
*/
}catch(Exception e){
System.out.println("反正就是出了个错!");
}
System.out.println("程序结束了");
}
}
/**
* finally块
* finally可以直接跟在try块之后,或者最后一个catch块之后.
* finally块可以保证无论try语句中的代码是否出错,
* 其finally块中代码一定被执行.
* 通常会将诸如释放资源等操作放入finally块
* @author Administrator
*
*/
public class ExceptionTest3{
public static void main(String[] args) {
System.out.println("程序开始了");
try {
String str = "";
System.out.println(str.length());
} catch (Exception e) {
System.out.println("出错了");
} finally{
System.out.println("finally中的代码被执行了!");
}
System.out.println("程序结束了");
}
}
/**
* 自定义异常
* 通常用来说明业务逻辑级别的异常
*
* 如自定义年龄不合法异常
* @author Administrator
*
*/
public class IllegalAgeException extends Exception{
private static final long serialVersionUID = 1L;
public IllegalAgeException() {
super();
// TODO Auto-generated constructor stub
}
public IllegalAgeException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public IllegalAgeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public IllegalAgeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public IllegalAgeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
/**
* 测试方法中的异常抛出
* @author Administrator
*
*/
public class Person {
private int age;

public int getAge() {
return age;
}

public void setAge(int age)throws IllegalAgeException {
if(age<0||age>100){
/*
* 当方法中使用throw抛出一个异常时,应当
* 在方法上使用throws声明该异常的抛出以
* 告知调用者来处理这个可能出现的异常.
* 只有抛出RuntimeException及其子类异常
* 时,可以不使用throws通知调用者.
*/
throw new IllegalAgeException("年龄不合法!");
}
this.age = age;
}
}
/**
* 测试异常抛出
* @author Administrator
*
*/
public class ExceptionTest4{
public static void main(String[] args) {
Person p = new Person();
try {
/*
* 当调用一个含有throws声明的方法时,
* 编译器要求我们必须处理该方法可能抛出的异常.
* 处理手段有两个:
* 1:使用try-catch自行捕获
* 2:在当前方法上继续使用throws将该异常抛出
*/
p.setAge(1000);
} catch (IllegalAgeException e){
e.printStackTrace();
}
System.out.println("年龄:"+p.getAge());

}
}

/**
* 子类重写父类含有throws抛出异常声明的方法时
* 对throws的重写要求:
* @author Administrator
*
*/
public class Father {
public void dosome()throws IOException,AWTException{

}
}
class Son extends Father{
//重写时可以不在抛出任何异常
// public void dosome(){
// }
//重写时可以只抛出部分异常
// public void dosome()throws IOException{
// }
//可以抛出父类方法抛出异常的子类异常
// public void dosome()
// throws FileNotFoundException{
// }
//不可以抛出额外异常
// public void dosome()throws SQLException{
// }
//不可以抛出父类方法抛出异常的父类异常
// public void dosome()throws Exception{
// }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: