Demonstration 10: Exceptional Handling
/* 1. Showing compile time error in a program. */
/* 2. Showing run-time errors in a program. */
class demo102
{
public static void main(String args[])
{
int a= [Link](args[0]);
int b= [Link](args[1]);
int c=a/b;
[Link]("Value of c =" + c);
}
}
/* Exception in thread "main" [Link]: Index 1(or
0) out of bounds for length 1(or 0) */
/* Exception in thread "main" [Link]: For input string:
"5.5" */
/* 3. Run the following program without exception-handling */
public class Demo103
{
static int function(int x, int y)
{
int a=x/y;
return a;
}
public static void main(String args[])
{
int a,b,result;
a=0;
b=0;
[Link]("Enter any two integers");
a=[Link](args[0]);
b=[Link](args[1]);
result=function(a,b);
[Link]("Result : " + result);
}
}
/* Exception in thread "main" [Link]: / by zero at
[Link]([Link])
*/
/* Run the following program with exception handling mechanism */
//Case : Simple try-catch block...
public class Demo104
{
static int function(int x, int y)
{
try
{
int a=x/y;
return a;
}
catch (ArithmeticException e)
{
[Link] ("Division by zero" );
}
return 0;
}
public static void main(String args[])
{
int a,b,result;
a=0;
b=0;
[Link]("Enter any two integers");
try{
a=[Link](args[0]);
b=[Link](args[1]);
}
catch(Exception e)
{}
result=function(a,b);
[Link]("Result : " + result);
}
}