0% found this document useful (0 votes)
38 views

This Is Java Experiment

The document contains 3 Java code examples that demonstrate try, catch, finally and throwing custom exceptions. The first example uses try, catch and finally to handle a divide by zero exception. The second example defines a custom exception class and throws it manually based on user input. The third example checks for incorrect password authentication and throws a custom exception if validation fails.

Uploaded by

Nikam Arnav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

This Is Java Experiment

The document contains 3 Java code examples that demonstrate try, catch, finally and throwing custom exceptions. The first example uses try, catch and finally to handle a divide by zero exception. The second example defines a custom exception class and throws it manually based on user input. The third example checks for incorrect password authentication and throws a custom exception if validation fails.

Uploaded by

Nikam Arnav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//1.Demonstrate use of finally,try,catch.

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
*/

//2.Throw Exception manually program.


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 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
*/

You might also like