class Demo
{
public int div(int x,int y)
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
int x=d.div(4,0); //0作为除数
System.out.println("x="+x);
System.out.println("over");
}
}
class Demo
{
public int div(int x,int y)
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
/*Demo d=new Demo();
int x=d.div(4,0);
System.out.println("x="+x);
System.out.println("over");
*/
byte[] arr=new byte[1024*1024*1000];
}
}
class Demo
{
public int div(int x,int y)
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
try
{
int x=d.div(4,0);
System.out.println("x="+x);
}
catch(Exception e)
{
System.out.println("除数有误");
}
System.out.println("over");
/*byte[] arr=new byte[1024*1024*1000];*/
}
}
class Demo
{
public int div(int x,int y)
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
try
{
int x=d.div(4,0);
System.out.println("x="+x);
}
catch(Exception e)
{
System.out.println("除数有误");
//获得异常信息
System.out.println(e.getMessage());
//获得异常信息,异常名称
System.out.println(e.toString());
//输出异常名称,异常信息,异常出现的位置
e.printStackTrace();
}
System.out.println("over");
/*byte[] arr=new byte[1024*1024*1000];*/
}
}
class Demo
{
public int div(int x,int y)throws Exception /*有可能出现异常的地方抛出异常*/
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
int x=d.div(4,0);
System.out.println("x="+x);
System.out.println("over");
}
}
class Demo
{
public int div(int x,int y)throws Exception /*有可能出现异常的地方抛出异常*/
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[]) throws Exception /*继续抛出异常,给虚拟机*/
{
Demo d=new Demo();
int x=d.div(4,0);
System.out.println("x="+x);
System.out.println("over");
}
}
class Demo
{
public int div(int x,int y)throws Exception /*有可能出现异常的地方抛出异常*/
{
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
try //自己处理异常
{
int x=d.div(4,0);
System.out.println("x="+x);
}
catch(Exception e)
{
System.out.println("除数有误");
//获得异常信息,异常名称
System.out.println(e.toString());
System.out.println("over");
}
}
}
class Demo
{
public int div(int x,int y)throws ArithmeticException,ArrayIndexOutOfBoundsException
{
int arr[]=new int [x];
System.out.println(arr[4]);
return x/y;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
try
{
int x=d.div(4,0);
System.out.println("x="+x);
}
catch(ArithmeticException e) /*除法法则异常对象接收,第一个执行*/
{
System.out.println("除数有误");
//获得异常信息,异常名称
System.out.println(e.toString());
System.out.println("over");
}
catch(ArrayIndexOutOfBoundsException e) /*数据越界的对象接收,第二个执行*/
{
System.out.println("数组越界了");
//输出异常信息
System.out.println(e.toString());
}
catch(Exception e) /*父类Exception接收,最后执行,建议不要写这个,让程序终止*/ /*用到了多态*/
{
System.out.println(e.toString());
}
}
}
class Demo
{
public int div(int x,int y)throws FuShuException /*抛出异常*/
{
if(y<0)
{
throw new FuShuException("分母出现负数了------/bu FuShu",y); /*自己手动抛出异常的对象*/
}
return x/y;
}
}
class FuShuException extends Exception
{
private int value;
FuShuException(String m,int value)
{
super(m); /*给父类Exception的getMessage方法传递参数*/
this.value=value;
}
public int getValue() /*自定义的方法,返回负数*/
{
return value;
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
try
{
int x=d.div(4,-3);
System.out.println("x="+x);
}
catch(FuShuException e) /*捕获异常对象*/
{
System.out.println(e.getMessage()+e.getValue());
}
System.out.println("over");
}
}
class Demo
{
public int div(int x,int y)throws FuShuException /*抛不抛结果都一样*/
{
if(y<0)
{
throw new FuShuException("分母出现负数了------/bu FuShu",y);
}
return x/y;
}
}
class FuShuException extends RuntimeException /*继承RuntimeException*/
{
FuShuException(String m,int value)
{
super(m);
}
}
class ExceptionDemo
{
public static void main(String args[])
{
Demo d=new Demo();
int x=d.div(4,-3); /*运行到这会出现异常,编译没有问题*/
System.out.println("x="+x);
System.out.println("over");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有