java学习——异常处理机制

时间:2023-11-13 23:56:14
public class ExceptionDemo2 {

    public static void main(String[] args) {
// TODO Auto-generated method stub int i=0;
int a[]={1,2,3,4};
for( i = 0; i<5; i++)
{
try
{
System.out.print("a[" + i + "]/"+ "=" + (a[i]/i));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("捕获数组下标越界");
}
catch(ArithmeticException e)
{
System.out.print("捕获算术异常");
}
finally
{
System.out.print("finally i=" + i);
}
System.out.println("继续!");
} } }

java学习——异常处理机制

public class ThrowsDemo {

    public static void throwOne() throws IllegalAccessException{
System.out.println("inside throwOne.");
throw new IllegalAccessException("ThrowsDemo");
} public static void main(String[] args) {
// TODO Auto-generated method stub try
{
throwOne();
}
catch(IllegalAccessException e)
{
System.out.println("Caught" + e);
}
} }

java学习——异常处理机制