Exception Handling
Any deviation from the current flow is termed as exception.
I have lived my days when my Mom used to give some money and instructions to buy list of items from the market. I happily took the money and paddled my cycle through the roads to the market unaware of exceptional condition that may confront me.
1) My cycle gets punctured
2) I run out of money
3) I don't get item(s) from the market and so on ...
These abnormal conditions which may stop me from completing my responsibilities are called exceptions. They may occur to me naturally or because of my irresponsible behavior. Now, I may be smart to be able to come out of these exceptional conditions.
1) I get the puncture repaired
2) I buy credit from the shop
3) these one, I may not be able to overcome ...
try, catch, throw
The truth that I try going to market and do things is similar to the code in the try block that is tried to run.
public class Myself{
public void doThingsForMom()
{
try{
// buy listed items
// your code goes here
} // end try block
} // end method
} // end class
I say the nature threw these conditions upon me is similar to the exceptions that JVM throws in my class.
The truth that I tried to handle the exceptional situation is similar to the catch block that catches the exception that gets throwed.
// when Mom asks me toDoThings I let her know first hand that I may be late ... so she prepares herself that I may get late
{
dearSon.doThingsForMom();
// so now mom needs to handle the exceptional condition when her son will be late
}catch(IMayBeLateException ex)
Any deviation from the current flow is termed as exception.
I have lived my days when my Mom used to give some money and instructions to buy list of items from the market. I happily took the money and paddled my cycle through the roads to the market unaware of exceptional condition that may confront me.
1) My cycle gets punctured
2) I run out of money
3) I don't get item(s) from the market and so on ...
These abnormal conditions which may stop me from completing my responsibilities are called exceptions. They may occur to me naturally or because of my irresponsible behavior. Now, I may be smart to be able to come out of these exceptional conditions.
1) I get the puncture repaired
2) I buy credit from the shop
3) these one, I may not be able to overcome ...
try, catch, throw
The truth that I try going to market and do things is similar to the code in the try block that is tried to run.
public class Myself{
public void doThingsForMom()
{
try{
// buy listed items
// your code goes here
} // end try block
} // end method
} // end class
I say the nature threw these conditions upon me is similar to the exceptions that JVM throws in my class.
The truth that I tried to handle the exceptional situation is similar to the catch block that catches the exception that gets throwed.
public class Myself{
public void doThingsForMom()
{
try
{
// your code goes here
} // end try block
catch(OutOfMoneyException)
{
}
} // end method
} // end class
throw, throws
There is a unique thing that happens here. I become a MAN out of Mama's boy as I take the conditions and handle it myself and my mom does not even know a thing that happened to me.
But in programming a calling method would like to know the exceptions that occur instead the callee method handling it silently. i.e. there is a need to propagate the exception.
public class Son{
public void doThingsForMom() throws IMayBeLateException
// when Mom asks me toDoThings I let her know first hand that I may be late ... so she prepares herself that I may get late
{
try
{
// your code goes here
// if getting late - throw new IMayBeLateException();
// if running out of money - throw new OutOfMoneyException();
} // end try block
catch(OutOfMoneyException ex)
{
// buy credit
}
} // end method
} // end class
public class Mom
{
public void toDoList()
{
Son dearSon = new Son();
try{
dearSon.doThingsForMom();
// so now mom needs to handle the exceptional condition when her son will be late
}catch(IMayBeLateException ex)
{
// she does not get tensed
}
} // end method
} // end class
Custom Exception
You can create your own exception by extending(IS-A) the Exception class.
public class IMayBeLateException extends Exception
{
public void toString()
{
}
}
No comments:
Post a Comment