今天是我在乐字节学习的第十天啦,老师讲的是【异常处理机制】
今天老师讲了异常处理,咱们可以一起来探讨下哈!如果有哪里不对的地方可以指出来哦!
运行结果:为了更好的理解异常机制,里面输出了一些多余的话。勿喷
下面是源码:欢迎老司机试开
class FuShuIndexException extends Exception
{
FuShuIndexException(){}
FuShuIndexException(String s)
{
super(s);
System.out.println(“这是FuShuIndexException类的内容。。。。”);
}
}
class Demo
{
public int method(int[] arr,int Index)throws FuShuIndexException
{
if (Index<0)
{
throw new FuShuIndexException(“数组角标为负啦!!抛出异常。”);
}
return arr[Index];
}
}
public class ExceptionDemo
{
public static void main(String[] args)//throws FuShuIndexException
{
int[] arr=new int[3];
//int num=arr[-3];
Demo d=new Demo();
try
{
int x=d.method(arr,-10);
System.out.println(x);
}
catch(FuShuIndexException e)
{
System.out.println(“已捕获异常。。。catch”);
System.out.println(“print getMessage:”+e.getMessage());
System.out.println(“print e:”+e);
e.printStackTrace();//jvm默认的异常处理机制调用的就是这个方法
System.out.println(“角标为负异常,catch语句输出。”);
}
//System.out.println(num);
System.out.println(“over”);
}
ln(“角标为负异常,catch语句输出。”);
}
//System.out.println(num);
System.out.println(“over”);
}
}