2020/10/24 Java学习记录No.6
Posted cyades
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020/10/24 Java学习记录No.6相关的知识,希望对你有一定的参考价值。
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! "); } } 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! "); } } 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! "); } } 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! "); } } 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! "); } } 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的主要内容,如果未能解决你的问题,请参考以下文章
CS294-112 深度强化学习 秋季学期(伯克利)NO.6 Value functions introduction NO.7 Advanced Q learning