public static String getString(Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); return sw.toString(); }
You may like
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
public static String getString(Throwable t) { StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); return sw.toString(); }
public class ThrowableEx { static void print()throws Throwable{ try{ int a = 10/0; } catch(ArithmeticException e){ Throwable t1 = new Throwable(); t1.initCause(e); throw t1; } } public static void main(String args[]){ Throwable t1 = new Throwable(); try{ print(); } catch(Throwable t){ System.out.println("Exception \t\t Cause"); System.out.println(t + "\t" + t.getCause()); } } }
Exception Cause java.lang.Throwable java.lang.ArithmeticException: / by zero
public class ThrowableEx { static void print()throws Throwable{ try{ int a = 10/0; } catch(ArithmeticException e){ Throwable t1 = new Throwable("Divide by zero exception"); throw t1; } } public static void main(String args[]){ Throwable t1 = new Throwable(); try{ print(); } catch(Throwable t){ System.out.println(t); } } }
java.lang.Throwable: Divide by zero exception
public class ThrowableEx { static void print()throws Throwable{ try{ int a = 10/0; } catch(ArithmeticException e){ Throwable t1 = new Throwable("Divide by zero exception", e); throw t1; } } public static void main(String args[]){ Throwable t1 = new Throwable(); try{ print(); } catch(Throwable t){ System.out.println(t.getMessage() +"\t" + t.getCause()); } } }
Divide by zero exception java.lang.ArithmeticException: / by zero
public class ThrowableEx { static void print()throws Throwable{ try{ int a = 10/0; } catch(ArithmeticException e){ Throwable t1 = new Throwable(e); throw t1; } } public static void main(String args[]){ Throwable t1 = new Throwable(); try{ print(); } catch(Throwable t){ System.out.println(t); } } }
java.lang.Throwable: java.lang.ArithmeticException: / by zero
class CheckedEx extends Throwable{ CheckedEx(String s){ super(s); } CheckedEx(){ } void print()throws CheckedEx{ throw new CheckedEx("I am checked Exception"); } }
class ThrowableEx{ public static void main(String args[]){ CheckedEx obj = new CheckedEx(); obj.print(); } }
ThrowableEx.java:4: error: unreported exception CheckedEx; must be caught or declared to be thrown obj.print(); ^ 1 error
class ThrowableEx{ public static void main(String args[])throws Throwable{ CheckedEx obj = new CheckedEx(); obj.print(); } }
Exception in thread "main" CheckedEx: I am checked Exception at CheckedEx.print(CheckedEx.java:12) at ThrowableEx.main(ThrowableEx.java:4)
class ThrowableEx{ public static void main(String args[]){ CheckedEx obj = new CheckedEx(); try{ obj.print(); } catch(CheckedEx e){ System.out.println("Exception occured " + e); } } }
Exception occured CheckedEx: I am checked Exception
class ThrowableEx{ public static void main(String args[]){ CheckedEx obj = new CheckedEx(); try{ obj.print(); } catch(CheckedEx e){ System.out.println("Stack trace is "); e.printStackTrace(System.out); } } }
Stack trace is CheckedEx: I am checked Exception at CheckedEx.print(CheckedEx.java:12) at ThrowableEx.main(ThrowableEx.java:5)
import java.io.*; class MyResource{ String name; void exception1(){ throw new RuntimeException("I am exception 1"); } void exception2(){ throw new RuntimeException("I am exception 2"); } void exception3(){ throw new RuntimeException("I am exception 3"); } void exception4(){ throw new RuntimeException("I am exception 4"); } }
import java.io.*; class SuppressEx{ static Throwable suppressed = new Throwable(); static MyResource obj1 = new MyResource(); static void throwFirst(){ try{ obj1.exception1(); } catch(Exception e){ suppressed.addSuppressed(e); throwSecond(); } } static void throwSecond(){ try{ obj1.exception2(); } catch(Exception e){ suppressed.addSuppressed(e); throwThird(); } } static void throwThird(){ try{ obj1.exception3(); } catch(Exception e){ suppressed.addSuppressed(e); obj1.exception4(); } } public static void main(String args[])throws Exception{ try{ throwFirst(); } catch(Exception e){ System.out.println(e); } System.out.println("--------------------------"); System.out.println("Suppressed Exceptions are"); System.out.println("--------------------------"); Throwable[] suppressedExceptions = suppressed.getSuppressed(); for(int i=0; i < suppressedExceptions.length; i++){ System.out.println(suppressedExceptions[i]); } } }
java.lang.RuntimeException: I am exception 4 -------------------------------- Suppressed Exceptions are -------------------------------- java.lang.RuntimeException: I am exception 1 java.lang.RuntimeException: I am exception 2 java.lang.RuntimeException: I am exception 3
class Stack{ int size; Stack(int size){ if(size < 0 ) throw new NegativeArraySizeException("Stack size is less than zero"); if(size == 0) throw new NegativeArraySizeException("Stack size is zero"); this.size = size; System.out.println("Stack created with size " + size); } }
class StackTest{ public static void main(String args[]){ Stack s1; try{ s1 = new Stack(-10); } catch(Exception e){ System.out.println(e); } try{ s1 = new Stack(0); } catch(Exception e){ System.out.println(e); } try{ s1 = new Stack(10); } catch(Exception e){ System.out.println(e); } } }
java.lang.NegativeArraySizeException: Stack size is less than zero java.lang.NegativeArraySizeException: Stack size is zero Stack created with size 10