Thursday, November 6, 2014

Java block pro



Throws Clause

package com.myjava.exceptions;
public class MyThrowsClause
{
 public static void main(String a[])
{
        MyThrowsClause mytc = new MyThrowsClause();
        try
{
            for(int i=0; i<5 i="" span="">
{
                mytc.getJunk();
                System.out.println(i);
 }
 }
  catch (InterruptedException iex)
{
            iex.printStackTrace();
        }
    }
 public void getJunk() throws InterruptedException
{
        Thread.sleep(1000);
    }
}

Throw Clause

package com.myjava.exceptions;
public class MyExplicitThrow
{
 public static void main(String a[])
{
        try
{
            MyExplicitThrow met = new MyExplicitThrow();
            System.out.println("length of Junk is "+met.getStringSize("Junk"));
            System.out.println("length of Wrong is "+met.getStringSize("Wrong"));
            System.out.println("length of null string is "+met.getStringSize(null));
        }
 catch (Exception ex)
 {
            System.out.println("Exception message: "+ex.getMessage());
        }
    }
  public int getStringSize(String str) throws Exception
  {
        if(str == null)
 {
            throw new Exception("String input is null");
        }
        return str.length();
    }
}

Multiple Cache Blocks

package com.myjava.exceptions;
import java.net.MalformedURLException;
import java.net.URL;
public class MyMultipleCatchBlocks
{
public static void main(String a[])
{
        MyMultipleCatchBlocks mmcb = new MyMultipleCatchBlocks();
        mmcb.execute(1);
        mmcb.execute(2);
  }
  public void execute(int i)
  {
        try
  {
            if(i == 1)
  {
                getIntValue("7u");
    }
    else
    {
                getUrlObj("www.junksite.com");
       }
      }
  catch (NumberFormatException nfe)
  {
 System.out.println("Inside NumberFormatException... "+nfe.getMessage());
 }
 catch (MalformedURLException mue)
{
        System.out.println("Inside MalformedURLException... "+mue.getMessage());
 }
catch (Exception ex)
{
            System.out.println("Inside Exception... "+ex.getMessage());
        }
    }
 public int getIntValue(String num)
 {
        return Integer.parseInt(num);
    }
 public URL getUrlObj(String urlStr) throws MalformedURLException
 {
        return new URL(urlStr);
    }
}

Finally Block

package com.myjava.exceptions;
public class MyFinallyBlock
{
    public static void main(String[] a)
{
        try
{
            int i = 10/0;
 }
 catch(Exception ex)
{
            System.out.println("Inside 1st catch Block");
 }
 finally
{
            System.out.println("Inside 1st finally block");
 }
        try
{
            int i = 10/10;
 }
 catch(Exception ex)
{
            System.out.println("Inside 2nd catch Block");
 }
 finally
{
            System.out.println("Inside 2nd finally block");
        }
    }
}

Java Custom Exception

package com.myjava.exceptions;
public class MyOwnException
{
public static void main(String[] a)
{
        try
{
            MyOwnException.myTest(null);
 }
catch(MyAppException mae)
{
            System.out.println("Inside catch block: "+mae.getMessage());
        }
    }
  static void myTest(String str) throws MyAppException
  {
        if(str == null)
   {
            throw new MyAppException("String val is null");
        }
    }
}
class MyAppException extends Exception
{
private String message = null;
public MyAppException()
{
        super();
 }
 public MyAppException(String message)
 {
        super(message);
        this.message = message;
  }
 public MyAppException(Throwable cause)
 {
        super(cause);
    }
    public String toString()
   {
        return message;
    }
    public String getMessage()
   {
        return message;
    }
}

No comments:

Post a Comment