Exception handling exercises in java program with output
A common exception:divided by zero
program1
public class Divide
{
public static void main(String args[])
{
System.out.println("\nPROGRAM EXECUTION START HERE\n");
int NUM1,NUM2,RESULT;
NUM1=Integer.parseInt(args[0]);
NUM2=Integer.parseInt(args[1]);
RESULT=NUM1/NUM2;
System.out.println(NUM1+"/"+NUM2+"="+RESULT);
System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n");
}
}
Using try catch block:
program2
public class Divide1
{
public static void main(String args[])
{
System.out.println("\nPROGRAM EXECUTION START HERE\n");
int NUM1,NUM2,RESULT;
try
{
NUM1=Integer.parseInt(args[0]);
NUM2=Integer.parseInt(args[1]);
RESULT=NUM1/NUM2;
System.out.println(NUM1+"/"+NUM2+"="+RESULT);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n");
}
}
observe the both programs and its output carefully
Comments
Post a Comment