1.
public class Baulk {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
int result=3/0;
System.out.print(result);}
catch(ArithmeticException e) {
System.out.println("你好亲,出现算数异常");
}
finally {
System.out.println("程序结束");}
}
}
2.
package app;
public class Thundering {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String str="lili";
System.out.println(str+"年龄是:");
int age=Integer.parseInt("20");//类型转换
System.out.print(age);
int result=3/0;
System.out.print(result);}
catch(NumberFormatException e) {
System.out.println("你好亲,出现数字格式异常");}
catch(ArithmeticException e) {
System.out.println("你好亲,出现算术异常");}
finally {
System.out.println("程序结束");}
}
}
3.
package app;
public class Thundering {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String str="lili";
System.out.println(str+"年龄是:");
int age=Integer.parseInt("20");//类型转换
System.out.print(age);
int result=3/0;
System.out.print(result);}
catch(NumberFormatException e) {
System.out.println("你好亲,出现数字格式异常");}
catch(ArithmeticException e) {
System.out.println("你好亲,出现算术异常");}
finally {
System.out.println("程序结束");}
}
}
4.
public class myException extends Exception{
public myException(String message) {
super(message);
}
}
5.
public class Tran {
static int avg(int num1,int num2) throws myException{
if(num1<0||num2<0) {
// System.out.print("存在负数,无法计算");
throw new myException("存在负数,无法计算");}
if(num1>0||num2>0) {
throw new myException("大于一百,无法计算");}
return(num1+num2)/2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
int result=avg(32,6);
System.out.println(result);
}
catch(myException e) {
System.out.println(e);
}
}
}
6.
public class Shoot {
static void pop()throws NegativeArraySizeException{
int[]arr=new int[-3];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
pop();
}
catch(NegativeArraySizeException e) {
System.out.println("pop()方法抛出的一场");
}
}
}
7.
public class Myexception1 extends Exception {
String message;
public Myexception1(String ErrorMessagr) {
message=ErrorMessagr;}
public String getMessage() {
return message;
}
}
8.
import java.awt.color.CMMException;
public class Captor{
static int quotient(int x,int y)throws Exception{ //定义方法抛出异常
if(y<0){ //判断
throw new Exception("除数不能是负数"); //异常信息
}
return x/y; //返回值
}
public static void main(String args[]){
try{ //try语句块包含可能发生异常的语句
int result=quotient(3,-1); //调用方法quotient()
}catch(myException e){ //处理自定义异常信息
System.out.println(e.getMessage()); //输出异常信息
}catch(ArithmeticException e){ //处理ArithmeticException异常
System.out.println("除数不能为0"); //输出提示信息
}catch(Exception e){ //处理其他异常
System.out.println("程序发生了其他异常"); //输出提示信息
}
}
}