Creating Custom Packages & Handling Exceptions in Java, Slides of Java Programming

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

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Creating User Deifned Packages:
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
adrish.b@ardentcollaborations.com
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Creating Custom Packages & Handling Exceptions in Java and more Slides Java Programming in PDF only on Docsity!

Creating User Deifned Packages:

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]

By-Adrish Bhattacharyay

Phone:+91-

[email protected]

http://www.ardentcollaborations.com

CORE JAVA-EXCEPTION HANDLING

Exception Handling Fundamentals

Java has its built-in capability to ensure that

exceptions are handled within the java program.

Exceptions handled by:

Programmer

JVM(Java Runtime System)

Exception Handling Fundamentals

Basically Exceptions occurs due to some run-time

errors.

ex:

When to try to divide some number by zero.

When we try to connect to a database but the DSN is

not created in the system. i.e in case of external

resources.

Exception Handling Fundamentals

When Exception occurs Object Creation(thrown)

A method may choose to handle exception itself and

pass it on.

At the point exception is caught it can be processed or

can be passed again.

Exceptions can be generated by Java Runtime

System or can be manually generated by your

code.

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. throwit 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 catch finally

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); }

Multiple catch clauses

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); }

throw

Till now whatever be the Exceptions we handled are thrown by Java Runtime

System.

Now we will manually throw the Exception explicitly.

General Form

throw ThrowableInstance;Object of type Throwable or a subclass of

Throwable.

Two ways to obtain Throwable object Using parameters in catch block.

 Creating one with new Operator.

throw

Ex:

class ThrowDemo

public void demoproc()

try

throw new NullPointerException(―demo‖);

catch(NullPointerException e)

{S.O.P(―caught‖);

e.printStackTrace();

throw e;

throws

If a method is capable of causing an exception that it does not handle , it must

specify this behaviour so that callers of the method can ground themselves

against that exception.

You do this by including a throws clause in the method declaration. A throw

clause in the method declaration. A throws clause list s the types of exceptions

that a method might throw.

Necessary for all exceptions except error and RuntimeExceptions.

FORMAT:

type mthod-name(parameter list) throws exception-list { //body of method…. }

throws

Example:

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