* 听完这一课我了解了 有些功能会有些小缺陷 但是还能很有用,这时可以给这个功能
* 声明异常“throws Exception”
* 然后别的程序调用得时候有两种情况:1,直接抛出“throws Execption” 2,处理异常
* 建立在进行catch处理时,chatch中一定要定义具体处理方式。
* 不要简单定义一句e.printStackTrace(),
* 也不要简单得就书写一条输出语句。
* ..怎么处理异常..
* java提供了 特有得语句进行处理。
* try
* {
* 需要被检测得代码;
* }
* catch(异常类 变量)
* {
* 处理异常得代码;(处理方式)
* }
*
* finally
* {
* 一定会执行得语句;
* }
* 还有多异常处理
* */
class Demo
{ //声明这个功能可能有异常 两个异常 调用这个程序需要处理
int div (int a, int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
{
int[] arr = new int[a];
System.out.print(arr[4]);
return a/b;
}
}
class InnerClasst5
{
public static void main(String[] args)//这里一个也抛出异常 throws Exception
{
Demo d = new Demo();
try
{
int x = d.div(4, 1);
System.out.println("x="+x);
}
catch(ArithmeticException e)
{
System.out.println(e.toString());
System.out.println("被0除了");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.toString());
System.out.println("角标越界了");
}
/*这个可以取代上面两段但是不详细 也可以放到下面 父类异常catch块放到最下面 不要定义多余得catch块
catch(Exception e)
{
System.out.print("异常:"e.toString());
}*/
System.out.println("over");
}
}