Escolar Documentos
Profissional Documentos
Cultura Documentos
org/Ioa56B
2)
Declare your own java exception class.use this exception class in another
class (hint:use the throws keyword)
class EmployeeTest {
static void employeeAge(int age) throws MyOwnException{
if(age < 0)
throw new MyOwnException("Age can't be less than zero");
else
System.out.println("Input is valid!!");
}
public static void main(String[] args) {
try {
employeeAge(-2);
}
catch (MyOwnException e) {
e.printStackTrace();
}
}
}
3) write a java code that throw and handle NullPointerException
A very common case problem involves the comparison between a String variable and a
literal. The literal may be a String or an element of an Enum. Instead of invoking the
method from the null object, consider invoking it from the literal.
// A Java program to demonstrate that invoking a method
// on null causes NullPointerException
import java.io.*;
class GFG
{
public static void main (String[] args)
{
// Initializing String variable with null value
String ptr = null;
//package main.java;
5. write a program that handle an exception that might be thrown by the following code.
answer
String[]strings={"welcome!"};
try{
System.out.println(strings[1]);
catch(ArrayIndexOutOfBoundsException e){
Ans.
try{
division(34,0);
}
catch(ArithmeticException e){
return a/b;
7. Declare your own exception class that state raw marks cannot be greater than 100 in student grade
calculation.
Ans.
public InvalidMark(){
class Grade {
try{
calcgrade();
}
catch(InvalidMark e){
2)
Declare your own java exception class.use this exception class in another
class (hint:use the throws keyword)