标签:虚拟 getc 方式 tcl push java nts ror 异常类
声明抛出处理:
public static void main(String args[]) throws IOExceprion;
程序捕获处理:
通过使用try - catch - [finally]
语句块,用来对可能产生异常的代码产生的异常进行捕获,并根据其异常类型进行不同的操作。
import java.util.EmptyStackException; import java.util.Stack; class A{ int v = 6; public int getV() { return v; } } public class ExcpOp { public static void Arithmetic() { int a = 6, b = 0; try{ int c = a / b; } catch (ArithmeticException ae) { System.out.println(ae.getClass().getName()+" has been throw"); } finally { System.out.println("ArithmeticEp 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("NullPointer is over!\n"); } } public static void EmptyStack() { Stack s = new Stack(); try{ s.push(5); s.pop(); System.out.println("Pop 1"); s.pop(); System.out.println("Pop 2"); } catch (EmptyStackException ese) { System.out.println(ese.getClass().getName()+" has been throw"); } finally { System.out.println("EmptyStack 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("EmptyStack 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("NegativeArraySize is over!\n"); } } public static void main(String[] args) { ExcpOp.Arithmetic(); ExcpOp.EmptyStack(); ExcpOp.IndexOutOfBounds(); ExcpOp.NegativeArraySize(); ExcpOp.NullPointer(); } }
public class MyException extends Exception{ MyException(String msg) { super(msg); } public static void Throw(int a) throws MyException { if(a <= 666) { throw new MyException(" 输入不666 "); } } public static void main(String[] args) { int a = 660; try{ MyException.Throw(a); } catch (MyException me) { me.printStackTrace(); a = 668; } finally { System.out.println("此时 a = "+a); } } }
标签:虚拟 getc 方式 tcl push java nts ror 异常类
原文地址:https://www.cnblogs.com/axchml/p/13893428.html