码迷,mamicode.com
首页 > 编程语言 > 详细

2020/10/24 Java学习记录No.6

时间:2020-10-26 11:08:15      阅读:27      评论:0      收藏:0      [点我收藏+]

标签:ext   str   隐式   owa   super   system   new   ase   getname   

技术图片

 

 

 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();
        }
    }
}

 

2020/10/24 Java学习记录No.6

标签:ext   str   隐式   owa   super   system   new   ase   getname   

原文地址:https://www.cnblogs.com/cyades/p/13872760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!