javase中异常抓抛模型

package zhang.javase.TestException;

import java.util.Date;

/**

  • 异常excption throwable类是java中所有错误和异常的基类 throwable: >>error(错误) >>excption(异常)
  • 1.异常分为编译期间异常:api中除了编译期间异常都是运行时异常
  • 2.运行时异常:java.lang.RuntimeException
  • @作者:小章鱼

/
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;
/**

  • 常见的异常以及异常的处理
  • @作者:小章鱼
    *java中异常的处理模式是:“抛”"抓"模式,
    *当在程序执行的过程中出现异常的时候,这时会抛出一个指定类型的异常对象给调用者,这是自动抛出的异常
    *对于异常的处理,我们时候try catch 的模式来处理异常,让即使出现异常,后面的程序任然能够正常的 处理
    *finally:不管怎么样,在finally中的语句一定会执行
    *Exception e:异常对象的两个方法e.getMessage():获取异常的信息
    e.printStackTrace();获取异常栈的信息
    当存在多个catch语句的时候,扑抓住异常的时候,进入第一个catch语句中,这时后面的catch语句将不会执行
    catch语句的向后顺序关系
    1.当catch的异常时不相关的时候,对顺序没有任何的要求
    2.当catch的语句是相关的时候,一定要将更具体的写在catch语句块的第一位

*/
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;
/**

  • 异常处理的第二种方法
  • throws:当程序出现异常的时候,会自动的抛出一个异常对象
  • 如果这个时候不对其进行处理的话,=可以将该异常向上抛出,可以时候throws关键词,将异常对象抛给该方法的调用者,
  • 一直不处理的话,改异常,最终将会抛给java虚拟机
  • 语法:public static void fun1 () throws Exception{}在方法的括号的后面,在花括号的前面
  • @作者:小章鱼

*/
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);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值