package zhang.javase.TestException;
import java.util.Date;
/**
/
public class TestExcption {
public static void main(String[] args) {
int a = 0;
int b = 2;
// System.out.println(a/b+“此时没有异常”);
// 被除数不能够为0此时抛出一个异常java.lang.ArithmeticException: / by zero
// System.out.println(b/a);
// str对象被赋值为null后调用 indexof方法,出现空指针异常,抛出:java.lang.NullPointerException
// String str=new String(“zhangyukang”);
// str=null;
// str.indexOf(“a”);
//main方法中调用main方法出现栈溢出异常抛出:java.lang.StackOverflowError
//main(args);
/
* 异常的处理
*/
// int c=1;
// int d=0;
// try {
// int e=c/d;
// System.out.println(e);
// } catch (Exception e) {
// System.out.println(“被除数不能为0”);
// }
// System.out.println(“我是出现异常后的语句_____try catch 之后异常不影响后面的语句执行”);
String name=new String(“zhangyukang”);
name=null;
try {
int lenght=name.length();
System.out.println(lenght);
} catch (Exception e) {
System.out.println(“出现的空指针异常”);
}finally {
//finally语句一定会执行
System.out.println(“我是finally中的语句”);
}
System.out.println(“我是出现异常后的语句_____try catch 之后异常不影响后面的语句执行”);
}
}
2
package zhang.javase.TestException;
import java.util.Scanner;
import org.junit.Test;
/**
*/
public class TestExcption2 {
@Test
public void test1() {
try {
int a = 2;
int b = 0;
System.out.println(a / b);
} catch (Exception e) {
System.out.println("excption ");
}
}
@Test
public void test2() {
try {
String name;
name = null;
System.out.println(name.toString());
} catch (Exception e) {
System.out.println("出现的是空指针异常");
}
}
@Test
public void test3() {
try {
int[] array = new int[3];
System.out.println(array[3]);
}catch( RuntimeException e){
System.out.println("发生运行时的异常");
}
catch (Exception e) {
System.out.println("出现的是数组越界异常");
}
}
@Test
public void test4(){
try {
Object obj=new String("zhangyukang");
int a=(int)obj;
} catch (Exception e) {
System.out.println("出现的是强制转化类型的异常");
}
}
@Test
public void test5(){
try {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入一个整数");
int a=scanner.nextInt();
} catch (Exception e) {
System.out.println("出现的是输入的数据格式不匹配的异常");
}
}
}
3
package zhang.javase.TestException;
/**
*/
public class TestThrows {
public static void main(String[] args) {
try {
fun1();
} catch (Exception e) {
System.out.println(“出现异常了!!!”);
}finally{
System.out.println(“我是finally中的语句”);
}
}
public static void fun1 () throws Exception{
int a=0;
int b=2;
System.out.println(b/a);
}
}
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4
package zhang.javase.TestException;
/**
*/
public class TestThrow {
public static void main(String[] args) {
Circle circle1 = new Circle(12.2);
Circle circle2 = new Circle(13.2);
Circle circle3 = new Circle(11.2);
int result = circle1.compareTo(circle2);
System.out.println(result);
int reslut=circle2.compareTo(circle3);
System.out.println(reslut);
circle1.compareTo(new String(“zhangyuakng”));
}
}
class Circle implements Comparable {
private double riadus;
public double getRiadus() {
return riadus;
}
public void setRiadus(double riadus) {
this.riadus = riadus;
}
@Override
public String toString() {
return "Cicle [riadus=" + riadus + "]";
}
public Circle(double riadus) {
super();
this.riadus = riadus;
}
@Override
public int compareTo(Object o) {
if (this == o) {
return 0;
} else if (o instanceof Circle) {
Circle obj = (Circle) o;
if (this.riadus > obj.riadus) {
return 1;
} else if (this.riadus < obj.riadus) {
return -1;
} else {
return 0;
}
} else {
throw new MyException("你传入的对象不是cicle类型的");
}
}
}
5
package zhang.javase.TestException;
/**
*/
public class TestDiyexcption {
public static void main(String[] args) {
try{
throw new MyException(“这是我自己定义的异常”);
}catch(MyException e){
System.out.println(e.getMessage());
}
try {
throw new MyException("zhangyukang");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
package zhang.javase.TestException;
public class MyException extends RuntimeException{
//该异常的序列号
static final long serialVersionUID = -3387516994229948L;
public MyException(){
super();
}
public MyException(String msg){
super(msg);
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public MyException(Throwable cause) {
super(cause);
}
}