Java J2ME JSP J2EE Servlet Android

Java By Example: Exception Handling – Throwing exception from method

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics

The second way of exception handling is throwing the exception. This is done by adding throws with method name.

Syntax of throwing exception from method is

access_modifier return_type method_name(arguments) throws exception_list{

}

Here is a practical example of doing this..

public
class ExceptionExp3 {

    public
static
void main(String args[]){

        ExceptionExp3 excpExp = new ExceptionExp3();

        try{

            excpExp.tryThrows();

        }catch(ArrayIndexOutOfBoundsException ae){

            System.out.println("Exception Caught");

        }

    }

    public
void tryThrows() throws ArrayIndexOutOfBoundsException{

        String strArray[] = {"This","is", "an", "example", "of", "Java","exception" };

        

            System.out.println(strArray[0]);

            System.out.println(strArray[1]);

            System.out.println(strArray[3]);

            System.out.println(strArray[4]);

            System.out.println(strArray[5]);

            System.out.println(strArray[6]);

            System.out.println(strArray[7]);

System.out.println("This statement wont execute");


 

    

    }

}

The ArrayIndexOutOfBoundsException is thrown from method tryThrows() instead of handling the exception. In this case when exception occurred in tryThrows() method instead of terminating the program this method throw the exception to caller.

This program will give the following output..

This

is

example

of

Java

exception

Exception Caught

Java by Example: Exception handling – Basic Exception Handling example ‘try catch’

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics


Exception Handling: The basic way to cope with exception is using try ...catch.

The Syntax for using try...catch is


 

try{

    ...

    ...

    Code have to monitor for exception

    ...

    ...

}catch(Exception e){

    ...

    Exception action code

    ...

}


 

Here is an easy example to demonstrate the above case..

public
class ExceptionExp2 {


 

    public
static
void main(String args[]){

        String strArray[] = {"This","is","an","example","of","Java","exception" };

        try{

            System.out.println(strArray[0]);

            System.out.println(strArray[1]);

            System.out.println(strArray[3]);

            System.out.println(strArray[4]);

            System.out.println(strArray[5]);

            System.out.println(strArray[6]);

            System.out.println(strArray[7]);

            


 

        }catch(ArrayIndexOutOfBoundsException exp){

            System.out.println("Exception Occurred");

        }

        System.out.println("This will execute");

    }

}


 

If you run the above code it will give the following output …

This

is

example

of

Java

exception

Exception Occurred

This will execute

So exception is handled here by printing a message "Exception Occurred" and the program continues to execute the codes after the try ... catch block instead of terminating.

Java by Example: Exception Handling – Java Exception Basics

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics

Java provides strong way to cope with Errors and Exceptions. When an illegal operation occurred Java throws exception. And When exception occurs program execution ends if exception is not handled properly. There is a class below to understand Java Exception..


 

public
class ExceptionExp1 {


 

    public
static
void main(String args[]){

        String strArray[] = {"This","is", "an", "example", "of", "Java","exception" };

        System.out.println(strArray[0]);

        System.out.println(strArray[1]);

        System.out.println(strArray[3]);

        System.out.println(strArray[4]);

        System.out.println(strArray[5]);

        System.out.println(strArray[6]);

        System.out.println(strArray[7]);

        System.out.println("Program will terminate before executing this line");

    }

}


 

If you execute this program, the output will be-

This

is

example

of

Java

exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7

    at example.java.lang.ExceptionExp1.main(ExceptionExp1.java:13)

Here exception occurs before ends the program and terminates.

Oracle : Hierarchical Data/ Tree in Oracle

Oracle provides strong way to query tree like data or hierarchical data.

The syntax for query hierarchical data is-


 

SELECT columns

FROM table_name

WHERE where_condition

CONNECT BY PRIOR condition_for_next_level

START WITH start_condition

ORDER SIBLINGS BY columns


 

Let's try it with a practical example.    


 

CREATE
TABLE location(

    id NUMBER,

    name
VARCHAR2(100),

    parent NUMBER

);


 

INSERT
INTO location(id,name,parent) VALUES(1,'Earth',0);

INSERT
INTO location(id,name,parent) VALUES(2,'Asia',1);

INSERT
INTO location(id,name,parent) VALUES(3,'Europe',1);

INSERT
INTO location(id,name,parent) VALUES(4,'America',1);

INSERT
INTO location(id,name,parent) VALUES(5,'Africa',1);

INSERT
INTO location(id,name,parent) VALUES(6,
'Oceania',1);


 

INSERT
INTO location(id,name,parent) VALUES(7,'Bangladesh',2);

INSERT
INTO location(id,name,parent) VALUES(8,'India',2);

INSERT
INTO location(id,name,parent) VALUES(9,'Pakistan',2);

INSERT
INTO location(id,name,parent) VALUES(10,'Singapore',2);

INSERT
INTO location(id,name,parent) VALUES(11,'Malaysia',2);


 

INSERT
INTO location(id,name,parent) VALUES(12,'England',3);

INSERT
INTO location(id,name,parent) VALUES(13,'France',3);

INSERT
INTO location(id,name,parent) VALUES(14,'Turkey',3);

INSERT
INTO location(id,name,parent) VALUES(15,'Scotland',3);


 

INSERT
INTO location(id,name,parent) VALUES(16,'Mexico',4);

INSERT
INTO location(id,name,parent) VALUES(17,'Canada',4);

INSERT
INTO location(id,name,parent) VALUES(18,'USA',4);


 

INSERT
INTO location(id,name,parent) VALUES(19,'Dhaka',7);

INSERT
INTO location(id,name,parent) VALUES(20,'Rajshahi',7);

INSERT
INTO location(id,name,parent) VALUES(21,'Chittagong',7);


 

INSERT
INTO location(id,name,parent) VALUES(22,'Las Vegas',18);

INSERT
INTO location(id,name,parent) VALUES(23,'New Orlince',18);

INSERT
INTO location(id,name,parent) VALUES(24,'New York',18);

Now

SELECT * FROM location;

Will give..

ID        NAME            PARENT

-----------------------------------------------

1        Earth            0

2        Asia            1

3        Europe         1

4        America         1

5        Africa            1

6        Oceania        1

7        Bangladesh        2

8        India            2

9        Pakistan        2

10        Singapore        2

11        Malaysia        2

12        England        3

13        France            3

14        Turkey            3

15        Scotland        3

16        Mexico            4

17        Canada            4

18        USA            4

19        Dhaka            7

20        Rajshahi        7

21        Chittagong        7

22        Las Vegas        18

23        New Orlince        18

24        New York        18


 

Now

SELECT
lpad(' ',8*(level-1))||NAME AS name
FROM LOCATION

CONNECT
BY
PRIOR id = parent

START
WITH parent = 0

Will give the following result

NAME

------------

Earth

Asia

Bangladesh

Dhaka

Rajshahi

Chittagong

India

Pakistan

Singapore

Malaysia

Europe

England

France

Turkey

Scotland

America

Mexico

Canada

USA

Las Vegas

New Orlince

New York

Africa

Oceania


 

You can also use ORDER SIBLINGS BY to sort siblings

ORACLE - ALTER TABLE: Rename Table

Syntax:

ALTER TABLE table_name
RENAME TO new_table_name;


Example:
ALTER TABLE emp
RENAME TO emp_records;

This will change the table name from 'emp' to emp_record.