In the previous article i have discuss about Exception Handling.If you have not gone throw you can go by clicking on the link Exception Handling .We will learn more about about Exception Handling in this article.
Throwing Exceptions
Exceptions can be thrown anywhere within a code block using throw statement. The operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.
Example:-
double divide1(int v1, int v1) {
if( v2 == 0 ) {
throw "You are dividng by zero!";
}
return (v1/v2);
}
Catching Exceptions
The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch.
Example:-
try {
// your code
} catch( ExceptionName e ) {
// code to handle ExceptionName exception
}
Example Exception Handling using try,catch and throw
Comments