



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of creating user-defined packages in java, including the directory structure and compilation process. It also covers the basics of exception handling, including the definition, types, and java keywords for handling exceptions. Examples of using system packages and creating a custom exception subclass.
Typology: Slides
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























package mypack; public class Pack { public void show() { System.out.println("Inside mypack"); } } Directory name of the package should be as same as the package name From the source file directory compile in the following way:
javac -d. Pack.java Accessing Package import mypack.Pack; class PackageExample { public static void main(String arr[]) { Pack pack=new Pack(); pack.show(); } } www.ardentcollaborations.com [email protected]
Example of using system package: import java.util.Date; class { public static void main(String arr[]) { Date dt=new Date(); System.out.println (―Now=‖+dt); } } www.ardentcollaborations.com [email protected]
ASSIGNMENT: Create a packge. Define a method for addition in it. Now import the package in a class and add two numbers. Take the inputs from user using Scanner class of java.util package. www.ardentcollaborations.com [email protected]
Java Key Words for - Exception Handling try contains block of code that may throw exception. catch Catch the exception thrown. finally contains the block of code that must be executed in the program. throwit manually throws the Exception. throws when a method throws some exception we specify with throws key word. Ex: m1(int a,int b)throws IOException { throw new IOException(); }
try { int d=0; int a=42/d; S.O.P(a); } catch(ArithmeticException e) { e.printStackTrace(); } finally { int z=d*42; S.O.P(z); }
PSVM(String arr[]) { Scanner sc=new Scanner(System.in); int x=sc.nextLine(); int y=sc.nextLine(); int a[]={x,y}; int b=5; try { int x=a[2]/b-a[1]; } catch(ArithmeticException e) { System.out.println(e); } catch(ArrayIndexOutOfBoundException e) { e.printStackTrace(); } finally { int y=a[1]/a[ ]; System.out.println(“y=”+y); }
type mthod-name(parameter list) throws exception-list { //body of method…. }
class ThrowsDemo { static void throwOne() throws IllegalAccessException { S.O.P(―inside throwOne‖); throw new IllegalAccessException(―demo‖)‘ } public static void main(String arr[]) { try { throwOne(); } catch(IllegalAccessExample e) { e.printStackTrace(); } } docsity.com