در این sample نشان داده شده است که چگونه می توان کلاس Exception مورد نظر خود را نوشت و آن را Throw کرد:
public class MyException extends Exception
{
public String toString(String str1)
{
return str1;
}
}
public class Test
{
public static void meth1(int a, int b) throws MyException
{
if (a<b) throw new MyException();
}
public static void main(String arg[])
{
try
{
Test.meth1(1,2);
}catch(MyException e)
{
System.out.println(e.toString("It is my Exception Test"));
e.printStackTrace();
}
}
}