Learning code about Java exception handling

zhaozj2021-02-16  119

//Multinest.java

Class multinest {static void procedure () {Try {int C [] = {1}; C [42] = 99;} catch (arrayindexoutofboundsexception e) {system.out.println ("Array Index OOB: E); }}} Public static void main (string args []) {Try {Int a = args.length; system.out.println ("a =" a); int b = 42 / a; procedure ();} catch ArithmeticException E) {system.out.println ("DIV BY 0:" E);}}}

//Throwdemo.java

class ThrowDemo {static void demoproc () {try {throw new NullPointerException ( "de3mo");} catch (NullPointerException e) {System.out.println ( "caught inside demoproc"); throw e;}} public static void main ( String args []) {Try {Demoproc ();

//Throwdemo1.java

class ThrowsDemo1 {static void procedure () throws IllegalAccessException {System.out.println ( "inside procedure"); throw new IllegalAccessException ( "demo");} public static void main (String args []) {try {procedure (); } catch (illegalaccessException e) {system.out.println ("CAUGHT" E);}}}} catch (nullpointers}) {system.out.println ("RECAUGHT: E);}}}

//Finallydemo.java

class FinallyDemo {static void procA () {try {System.out.println ( "inside procA"); throw new RuntimeException ( "demo");} finally {System.out.println ( "procA's finally");}} static Void procb () {try {system.out.println ("inside procb"); return;} finally {system.out.println ("procb's finally");}}

Public static void main (string args []) {try {proca ();} catch (exception e) {procb ();}}}

转载请注明原文地址:https://www.9cbs.com/read-8543.html

New Post(0)