赞
踩
异常处理 //1. throw用于方法内部,主要表示手工异常抛出。 //2. throws主要在方法声明上使用,明确告诉用户本方法可能产生的异常,同时该方法可能不处理此异常。 public class Test { public static void code1(){ //System.out.println("1.数学计算开始前"); //System.out.println("2.进行数学计算:"+10/0); // ArithmeticException: / by zero //System.out.println("3.数学计算结束后"); //只能进行到第二步 //异常的处理try{ 有可能出现异常的语句 ; } catch (异常类 对象) { } finally {异常的出口 } //try..catch 、 try..finally 、 try..catch..finally System.out.println("1.数学计算开始前"); try{ System.out.println("2.进行数学计算:"+10/0); }catch(ArithmeticException e){ System.out.println("此处出现ArithmeticException异常,已经被处理,继续执行下一步"); } System.out.println("3.数学计算结束后"); }
public static void code2(){ //获取异常的完整信息 System.out.println("1.数学计算开始前"); try{ System.out.println("2.进行数学计算:"+10/0); }catch(ArithmeticException e){ e.printStackTrace(); //显示异常信息,具体出现了什么异常。 } System.out.println("3.数学计算结束后"); } public static void code3(){ //异常处理。finally语句,不算这个语句是否产生异常,都会输出finall里面的东西 System.out.println("[1].数学计算开始前"); try{ System.out.println("[2].进行数学计算:"+10/0); }catch(ArithmeticException e){ e.printStackTrace(); //显示异常信息,具体出现了什么异常。 }finally { System.out.println("finally部分,不管是否产生异常,都会进入finally部分"); } System.out.println("[3].数学计算结束后"); } public static void code4(){ } public static void main(String[] args) { code1(); code2(); code3(); code4(); //由控制台输入x,y的值,有可能产生三种情况的异常 //用户没有输入初始化参数:ArrayIndexOutOfBoundsException //用户输入的不是数字: NumberFormatException //被除数为0:ArithmeticExceptio //所以这个时候就需要抛出多种异常 System.out.println("[1].数学计算开始前"); try{ int x = Integer.parseInt(args[0]) ; int y = Integer.parseInt(args[1]) ; System.out.println("[2].进行数学计算:"+x/y); }catch(ArithmeticException e){ e.printStackTrace(); }catch(NumberFormatException e){ e.printStackTrace(); }catch(ArrayIndexOutOfBoundsException e){ e.printStackTrace(); }finally{ System.out.println("finally部分"); } System.out.println("[3].数学计算结束后"); } }
//如果现在调用了throws声明的方法,那么在调用时必须明确的使用try…catch…进行捕获
//因为该方法有可能产生异常,所以必须按照异常的方式来进行处理。
//主方法上也可以使用throws进行异常抛出,这个时候如果产生了异常就会交给 JVM处理。
public class Test1 {
public static int calculate(int x,int y) throws Exception{
return x/y ;
}
public static void main(String[] args) throws Exception{
System.out.println(calculate(10, 0));
}
}
//throw
//一般而言throw和break、continue、break都要和if结合使用。
public class test2 {
public static void main(String[] args){
try{
throw new Exception("throw");
}catch (Exception e){
e.printStackTrace();
}
}
}
//现在要求编写一个方法进行除法操作,但是对于此方法有如下要求:
// 1. 在进行除法计算操作之前打印一行语句"**"。
// 2. 如果在除法计算过程中出现错误,则应该将异常返回给调用处。
// 3. 不管终是否有异常产生,都要求打印一行计算结果信息。
public class Test3 { public static int calculate(int x,int y) throws Exception{ int result=0; System.out.println("*计算开始*"); try{ result = x/y ; }catch (Exception e){ System.out.println("计算中发现Exception异常,已经被处理"); }finally { System.out.println("计算中"); } return result; } public static void main(String[] args){ try{ System.out.println(calculate(10, 5)); }catch (Exception e){ e.printStackTrace(); }finally{ System.out.println("计算结束"); } try{ System.out.println(calculate(10, 0)); }catch (Exception e){ e.printStackTrace(); }finally{ System.out.println("计算结束"); } } }
//RuntimeException类
//parseInt的源码定义:public static int parseInt(String s) throws NumberFormatException //这个方法上已经明确抛出异常,但是在进行调用的时候发现,即使没有进行异常处理也可以正常执行。这个就属于 RuntimeException的范畴。 //使用RuntimeException定义的异常类可以不需要强制性进行异常处理。 //请解释Exception与RuntimeException的区别,请列举几个常见的RuntimeException: //1. 使用Exception是RuntimeException的父类,使用Exception定义的异常都要求必须使用异常处理,而使用RuntimeException定义的异常可以由用户选择性的来进行异常处理。 //2. 常见的RuntimeException:ClassCastException、NullPointerException等。 public class Test4 { public static void main(String[] args){ String str="100"; int num=Integer.parseInt(str); System.out.println(num); } }
//自定义异常
//自定义异常类可以继承两种父类:Exception、RuntimeException。 //eg:如果两个数相加等于50.则抛出一个名为AddExcep异常 public class Test5 { public static void main(String[] args) throws Exception { int num1=20; int num2=30; if(num1+num2==50){ throw new AddException("这是一个相加为50的自定义抛出异常"); } } } class AddException extends Exception{ public AddException(String msg){ super(msg); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。