This Is Java Experiment
This Is Java Experiment
class Arithmaticdemo
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
int c=10/0;
System.out.println("NO Output");
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("In finally block");
}
}
}
/*
D:\Diploma\CO_4-I\JAVA\practical>javac 23_25_1.java
D:\Diploma\CO_4-I\JAVA\practical>java Arithmaticdemo
java.lang.ArithmeticException: / by zero
In finally block
*/
class myexceptiondemo
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String name;
int age;
try{
System.out.println("Enter your name");
name=br.readLine();
System.out.println("Enter your age");
age=Integer.parseInt(br.readLine());
if(age<0)
{
throw new myexception("Entered age is negative");
}
else
{
throw new myexception("Entered age is correct");
}
}
catch(myexception e)
{
System.out.println(e);
}
catch(Exception e1)
{
System.out.println(e1);
}
}
}
/*/
OUTPUT:
D:\Diploma\CO_4-I\JAVA\practical>javac 23_25_2.java
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter your name
abc
Enter your age
18
myexception: Entered age is correct
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter your name
abc
Enter your age
-8
myexception: Entered age is negative
*/
//3.develop program for authentication fail exception if the password is
incorrect.
import java.io.*;
class myexception extends Exception
{
myexception(String msg)
{
super(msg);
}
}
class myexceptiondemo
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String password;
String pass="abc";
try{
System.out.println("Enter password");
password=br.readLine();
if(!pass.equals(password))
{
throw new myexception("Authentication fail");
}
else
{
throw new myexception("correct password");
}
}
catch(myexception e)
{
System.out.println(e);
}
catch(Exception e1)
{
System.out.println(e1);
}
}
}
/*
OUTPUT:
D:\Diploma\CO_4-I\JAVA\practical>javac 23_25_3.java
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter password
abc
myexception: correct password
*/