📄 userexceptiondemo.java
字号:
class ArraySizeException extends NegativeArraySizeException
{
ArraySizeException()
{
super("您传递的是非法数组大小");
}
}
class UserExceptionDemo
{
int size,array[];
UserExceptionDemo(int s)
{
size=s;
try
{
checkSize();
}
catch(ArraySizeException e)
{
System.out.println(e);
}
}
void checkSize() throws ArraySizeException//方法将可能引发自定义异常
{
if (size<0)
throw new ArraySizeException();//若数组大小为负数,抛出自定义异常
array=new int[size];
for (int i=0;i<size;i++)
{
array[i]=i+1;
System.out.print(array[i]+" ");
}
System.out.println();
}
//主方法
public static void main(String[] args)
{
System.out.println("第一次传入10:");
new UserExceptionDemo(10);
System.out.println();
System.out.println("第二次传入-10:");
new UserExceptionDemo(-10);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -