Discuss any five exception classes in Java.


All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the Exception class, there is another subclass called Error which is derived from the Throwable class. The Exception class has two main subclasses: IOException class and RuntimeException class. There are many subclasses of RuntimeException class such as ArithmeticException, NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, ClassCastException, etc. The ArrayIndexOutOfBoundsException class extends IndexOutOfBoundsException class. The exception hierarchy is shown below:


Some of the exception classes in Java are discussed below:

1.ArithmeticException class:

This class deals with exceptional arithmetic conditions such as the divide-by-zero exception. The constructors for this class are:

a. public ArithmeticException()

b. public ArithmeticException(String s)


//Code Example public class ExceptionTest {

public static void main(String[] args) {

try {

int a = 10;

int b = 0;

int c = a / b;

} catch (ArithmeticException e) {

System.out.println(e);

}

}

}


ii. ArrayIndexOutOfBoundsException class:

This class is a subclass of IndexOutOfBoundsException class which is a subclass of RuntimeException class. This class is used to deal with the accessing of an illegal index of an array. The illegal index means either negative value or a value greater than or equal to the size of the array. The constructors for this class are:

a. public ArrayIndexOutOfBoundsException()

b. public ArrayIndexOutOfBoundsException(int index)

c. public ArrayIndexOutOfBoundsException(String s)


//Code Example public class ExceptionTest {

public static void main(String[] args) {

try {

int a[] = new int[10];

int size = -10;

if (size < 0) {

throw new ArrayIndexOutOfBoundsException("Negative Index");

}

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println(e.getMessage());

}

}

}


3.NullPointerException class:

This class is used to deal with the cases where there should be an object instead of null. For example, when we try to find the length of string when the string contains null, then this exception occurs. The constructors for this class are:

a. public NullPointerException()

b. public NullPointerException(String s)


//Code Example public class ExceptionTest {

public static void main(String[] args) {

try {

String s = null;

int len = s.length();

} catch (NullPointerException e) {

System.out.println(e);

}

}

}


4.ClassCastException class:

This class deals with the code that attempts to cast an object to a subclass of which it is not an instance. For example, when we try an integer object to be casted to a string object, then this exception occurs. The constructors for this class are:

a. public ClassCastException()

b. public ClassCastException(String s)


//Code Example public class ExceptionTest {

public static void main(String[] args) {

try {

Object o = new Integer(5);

System.out.println((String)o);

} catch (ClassCastException e) {

System.out.println(e);

}

}

}


v. IllegalArgumentException class:

This class deals with methods that are passed an illegal or inappropriate argument. The constructors for this class are:

a. public IllegalArgumentException()

b. public IllegalArgumentException(String s)


//Code Example public class ExceptionTest {

static void myAge(int age){

if(age<0 || age>99){

throw new IllegalArgumentException("Invalid Age");

}

}

public static void main(String[] args) {

try {

myAge(111);

} catch (IllegalArgumentException e) {

System.out.println(e);

}

}

}

Comments

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Explain network topology .Explain tis types with its advantages and disadvantges.