|
| 1 | +import java.io.Closeable; |
| 2 | +import java.io.IOException; |
| 3 | + |
| 4 | +/** |
| 5 | + * str.matches(regExp) return tru if str matches the given regular expressions. |
| 6 | + * |
| 7 | + * RuntimeException extends Exception extends Throwable. |
| 8 | + * StackOverflowError extends Error extends Throwable. |
| 9 | + * |
| 10 | + * in a try with resources block, when an exception is thrown, the resource is closed before any catch block. |
| 11 | + * |
| 12 | + */ |
| 13 | +public class Challenge_10 { |
| 14 | + public static void main( String[] args ) { |
| 15 | + String soprano = null; |
| 16 | + CloseIt closeIt = new CloseIt(); |
| 17 | + try (closeIt){ |
| 18 | + System.out.println(soprano.matches(null)); |
| 19 | + }catch (RuntimeException re){ |
| 20 | + try (closeIt){ |
| 21 | + System.out.println("runtime"); |
| 22 | + throw new StackOverflowError(); |
| 23 | + } |
| 24 | + catch(Exception e){ |
| 25 | + System.out.println("exception"); |
| 26 | + } |
| 27 | + } |
| 28 | + catch (Error error){ |
| 29 | + System.out.println("error"); |
| 30 | + } |
| 31 | + catch (Throwable throwable){ |
| 32 | + System.out.println("throwable"); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static class CloseIt implements Closeable { |
| 37 | + @Override |
| 38 | + public void close() throws IOException { |
| 39 | + System.out.println("close"); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments