java的异常分为:运行时异常和编译时异常(也可以说是非运行时异常)。
 throws:
     1)并没有真正的处理异常,只是骗过编译器。
     2)逐层的上抛异常,直到main方法。如果main中仍没有处理,程序会被异常中断。
     3)throws中断了本层的代码。
     4)throws运行时异常没有意义
 throw:throw 关键字,后跟异常对象,直接将异常对象抛出。
 自定义异常:只能用throw抛出。分两步:
     1)继承自Exception或者Exception的子类。
     2)提供String做参数的构造,利用父类的String做参数的构造完成初始化,String内容用于对异常的描述。

    下面代码是描述自定义异常的:

    ReportNotFoundException.java为自定义的一种异常类继承了Exception

package day08;
public class ReportNotFoundException extends Exception{
public ReportNotFoundException(){
}
public ReportNotFoundException(String msg){
super(msg);
}
}

    接下来是带有异常处理的代码:是对以上的测试:

package day08;
public class Employee {
private String name;
public Employee(){
}
public Employee(String name){
this.name = name;
}
public String getReport() throws ReportNotFoundException{
if(Math.random()>0.7){
throw new ReportNotFoundException(name+"找不到报表!");
}
return name+"找到报表了!";
}
}
class Manager {
private Employee[] em;
public Manager(){
}
public Manager(Employee[] em){
this.em = em;
}
public String getReport() throws ReportNotFoundException{
StringBuffer sb = new StringBuffer();
for(int i = 0;i < em.length; i++){
sb.append(em[i].getReport());
}return sb.toString();
}
}
class CFO {
private Manager[] ma;
public CFO(){
}
public CFO(Manager[] ma){
this.ma = ma;
}
public String getReport(){
try{
return ma[0].getReport();
}catch(ReportNotFoundException re){
re.printStackTrace();
}
return null;
}
}
class CEO {
private CFO cfo;
public CEO(CFO cfo){
this.cfo = cfo;
}
public String getReport(){
return cfo.getReport();
}
}

    接下来是对以上进行的测试:

package day08;
public class Test {
public static void main(String[] args) {
Employee j = new Employee("maidou");
Employee k = new Employee("doudou");
Employee l = new Employee("guaiguai");
Employee[] e = new Employee[]{j,k,l};
Manager[] m = new Manager[]{new Manager(e)};
CFO cfo = new CFO(m);
CEO ceo = new CEO(cfo);
ceo.getReport();
}
}

    对于编译时一场来说:一般都是用try-catch-finally来处理掉。方法中可以不处理,用throws上抛,最终会抛给main函数,那么main函数最终也要上抛,最终会抛给虚拟机,让虚拟机进行自行处理,显然这样是不好的,所以main函数一定要处理掉异常了。

    对于运行时异常:可以用try-catch-finally处理,但是对效率和代码阅读都有影响,所有一般都是用if条件来使程序健壮。

    try-catch-finally结构如下:

    try{//只出现一次且为可能出现异常的代码

    }catch(异常对象){//可出现多次,当多次的时候小的写上面,大的写下面,异常时从上到下逐步的捕获的;捕获到的对应的异常对象之后所做的处理

    }finally{出现0-1次,没有catch是finally一定要有,而且是一定会执行的代码

    }

    通过List、set等接口有多个实现类:

    ArrayList:效率高、不直接支持并发,多用于查询

    Vector:效率低、线程安全,直接支持兵法操作,多用于查询

    以上两个实现类类似于StringBuilder和StringBuffer

    HashSet:存储效率高、查询效率低,可去重

    TreeSet:存储效率低、查询效率高,而且会自动调用Compare方法进行自动排序

    HashMap、HashTable、TreeMap等等就以后再说吧!

    大部分实现类都有增删改查的方法,下面看一个关于自定义泛型的小例子吧:

    实体类Student.java

package day08;
public class Student implements Comparable<Student>{
private int id;
private String name;
public Student() {
super();
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
System.out.println(this.name+":hashCode");
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println(this.name+":equals");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
}

    对输入的信息通过比较字符串排序,其底层而是通过比较字符的ascII码值来进行的排序

    下面是测试类TestStudent.java

package day08;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class TestStudent {
public static void main(String[] args) {
Set<Student> set = new HashSet<Student>();//HashSet判断元素是否存在,靠hashCode和equals。
Student stu1 = new Student(1,"guaiguai");//而且Set中不可以重复
Student stu2 = new Student(2,"maidou");
Student stu3 = new Student(3,"xiaodoudou");
Student stu4 = new Student(2,"maidou");
set.add(stu1);
set.add(stu2);
set.add(stu3);
set.add(stu4);
System.out.println(set);
System.out.println(set.size());
System.out.println("---------------------------------------");
List<Student> list = new ArrayList<Student>();//ArrayList判断元素是否存在,或者删除元素,都是靠equals。
Student stu11 = new Student(1,"guaiguai");//ArryaList中可以重复存在
Student stu21 = new Student(2,"maidou");
Student stu31 = new Student(3,"xiaodoudou");
Student stu41 = new Student(2,"maidou");
list.add(stu11);
list.add(stu21);
list.add(stu31);
list.add(stu41);
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}

    好啦就先到这里吧,明天见啦!