Lab Programs
Lab Programs
Lab Programs
Exercise - 7 (Exception)
a)
Aim: To write a JAVA program that describes exception handling
mechanism
Program:
class trydemo
{
public static void main(String args[])
{
try
{
int a=10,b=0;
int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
}
Output:
java.lang.ArithmeticException: / by zero
After the catch statement
b) Illustrating multiple catch classes
Program:
Aim: To write a JAVA program Illustrating Multiple catch clauses
class multitrydemo
{
public static void main(String args[])
{
try
{
int a=10,b=5;
int c=a/b;
int d[]={0,1};
System.out.println(d[10]);
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
}
Output:
java.lang.ArrayIndexOutOfBoundsException: 10
After the catch statement
Exercise – 12
a) Illustration of class path
Aim: To write a JAVA program illustrate class path
import java.net.URL;
import java.net.URLClassLoader;
public class App
{
public static void main(String[] args)
{
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< urls.length; i++)
{
System.out.println(urls[i].getFile());
}
}
}
Output:
E:/java%20work/
b) A case study on including in class path in os environment
Aim: To write a case study on including in class path in your os
environment of your package.
* The differences between path and classpath are given by.
(1) The PATH is an environment variable used to locate "java" or
"javac" command, to run java
program and compile java source file. The CLASSPATH is an
environment variable used to set path for java classes.
(2) In order to set PATH in Java, we need to include bin directory in
PATH environment while in order to set CLASSPATH we need to
include all directories where we have put either our .class file or JAR
file, which is required by our Java application.
(3) PATH environment variable is used by operating system while
CLASSPATH is used by Java ClassLoaders to load class files.
(4) Path refers to the system while classpath refers to the Developing
Environment.
* By default the java run time system uses the current working
directory.
* Normally to execute a java program in any directory we have to set the
path by as follows
set path= c:\Program Files\java\jdk1.9.0_10\bin;
c) Creating and importing a package
Aim: To write a JAVA program that import and use the defined your
package in the previous
Problem
(i) Creating a package:
Steps:
1. First declare the name of the package using package keyword
Example: package mypack;
2. Type the following program under this package statement. In package
: class ,data, methods all are public
package mypack;
public class box
{
public int l=10,b=20;
public void display()
{
System.out.println(l);
System.out.println(b);
}
}
3. Create sub directory with a name same that of package name under
the current working directory by as follows. d:\>md mypack
4. Under this subdirectory store the above program with a file name
“box.java”.
(ii) importing a package:
Steps:
1. packages can be accessed by using the import statement
General form: import pack1[.pack2].(classname/*);
Example: import java.io.*;
Here pack1 is name of top level package and pack2 is name of sub
package
2. Type the following program under the current working directory and
save the program with a
file name “example.java”.
import mypack.box;
class packagedemo
{
public static void main(String args[])
{
box b1=new box();
b1.display();
}
}
3. Now compile the above program in the current working directory d:\
javac packagedemo.java
4. Execute the above program in current working directory
java packagedemo
Output:
10
20