started with new topic exception handling here is a simple example to start with
it is a division programme that divides two numbers and if denominator is 0 it shows error if catch exception is not there
1-
import java.io.*;
class se{
public static void main(String arg[])
{
DataInputStream dis=new DataInputStream(System.in);
try{
int a,b;
a=Integer.parseInt(dis.readLine());
b=Integer.parseInt(dis.readLine());
int c=a/b;
System.out.println(""+c);
}
catch(Exception e)
{
System.out.println("you cant do that");
}
}
}
it is a division programme that divides two numbers and if denominator is 0 it shows error if catch exception is not there
1-
import java.io.*;
class se{
public static void main(String arg[])
{
DataInputStream dis=new DataInputStream(System.in);
try{
int a,b;
a=Integer.parseInt(dis.readLine());
b=Integer.parseInt(dis.readLine());
int c=a/b;
System.out.println(""+c);
}
catch(Exception e)
{
System.out.println("you cant do that");
}
}
}
No comments:
Post a Comment