1.他们是Throwable的两个平行类。Expection是所有异常类的祖先,而Error是错误类的祖先。

  ①Error不是程序需要捕获和处理的,发生时程序将会停止。

  ②Exception有许多子类,都是按照包的形式组织的,程序需要应对这些异常对象进行相应的处理。

2.

 

 

 

//隐式声明抛出
1
import java.util.*;
2 class TestEmptyStack { 3 public static void main(String[] args){ 4 Stack st = new Stack(); 5 Object ob = st.pop(); 6 } 7 }
//显示声明抛出
import
java.io.*; class TestScreenIn{ public static void main(String[] args) throw IOException{ BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in)); String c1; int i=0; String[] e = new String[10]; while(i<10){ c1 = keyin.readLine(); e[i] = c1; i++; } } }

 

 3.

package com.company;

import java.util.EmptyStackException;
import java.util.Stack;

class A{
    int v = 1;
    public int getV() {
        return v;
    }
}

public class test {
    public static void Arithmetic() {
        int a = 1, b = 0;
        try{
            int c = a / b;
        } catch (ArithmeticException ae) {
            System.out.println(ae.getClass().getName()+" has been throw");
        } finally {
            System.out.println("ArithmeticException is over!\n");
        }
    }

    public static void NullPointer() {

        try {
            A a = null;
            a.getV();
        } catch (NullPointerException npe) {
            System.out.println(npe.getClass().getName()+" has been throw");
        } finally {
            System.out.println("NullPointerException is over!\n");
        }
    }

    public static void EmptyStack() {
        Stack s = new Stack();

        try{
            s.pop();
            System.out.println("Pop");
        } catch (EmptyStackException ese) {
            System.out.println(ese.getClass().getName()+" has been throw");
        } finally {
            System.out.println("EmptyStackException is over!\n");
        }
    }

    public static void IndexOutOfBounds() {
        int[] a = new int[3];
        for (int i = 0; i<3 ; i++ ) {
            a[i] = i;
        }
        try{
            System.out.println(a[4]);
        } catch (IndexOutOfBoundsException ioe) {
            System.out.println(ioe.getClass().getName()+" has been throw");
        } finally {
            System.out.println("IndexOutOfBoundsException is over!\n");
        }
    }

    public static void NegativeArraySize() {
        try{
            int[] a = new int[-3];
        } catch (NegativeArraySizeException nase) {
            System.out.println(nase.getClass().getName()+" has been throw");
        } finally {
            System.out.println("NegativeArraySizeException is over!\n");
        }
    }

    public static void main(String[] args) {
        test.Arithmetic();
        test.EmptyStack();
        test.IndexOutOfBounds();
        test.NegativeArraySize();
        test.NullPointer();
    }

}

 

 

4.

 

 

 

import java.util.*;

public class myexception extends Exception{
    myexception(String msg){
        super(msg);
    }
    static void throwOne(String c) throws myexception{
        //String a = "cya";
        if(c.equals("cya")){
            throw new myexception("you cant say that");
        }
    }
    public  static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        try {
            myexception.throwOne(s);
        }catch (myexception b){
            b.printStackTrace();
        }
    }
}

 

posted on 2020-10-25 11:47  studycya  阅读(73)  评论(0)    收藏  举报