Você está na página 1de 6

http://ide.geeksforgeeks.

org/Ioa56B

1)write a program that handle an exception that might be thrown by the


following code

public class Main


{
public static void main(String[] args)
{
int[] a={1,2,0};
try
{
System.out.println(a[3]/a[1]);
}
catch(Exception e)
{
System.out.println("You can not divide like this number by zer");
}
//System.out.println("You can not divide like this number by zero");
}
}

2)
Declare your own java exception class.use this exception class in another
class (hint:use the throws keyword)

public class MyOwnException extends Exception {


public MyOwnException(String msg)
{
super(msg);
}
}

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

String comparison with literals

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;

// Checking if ptr.equals null or works fine.


try
{
// This line of code throws NullPointerException
// because ptr is null
if (ptr.equals("gfg"))
System.out.print("Same");
else
System.out.print("Not Same");
}
catch(NullPointerException e)
{
System.out.print("NullPointerException Caught");
}
}
}

4) write a java code that throw and handle classnotfoundexception

//package main.java;

public class ClassNotFoundExceptionExample {

private static final String CLASS_TO_LOAD = "main.java.Utils";

public static void main(String[] args) {


try {
Class loadedClass = Class.forName(CLASS_TO_LOAD);
System.out.println("Class " + loadedClass + " found
successfully!");
}
catch (ClassNotFoundException ex) {
System.err.println("A ClassNotFoundException was
caught: " + ex.getMessage());
ex.printStackTrace();
}
}

5. write a program that handle an exception that might be thrown by the following code.

answer

public class Main {

public static void main(String[] args) {

String[]strings={"welcome!"};

try{

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

catch(ArrayIndexOutOfBoundsException e){

System.out.println("there was an array index out of bound");

6. Try to call the division method in the main method.

Ans.

public class Test {

public static void main(String[] args) {

try{

division(34,0);

}
catch(ArithmeticException e){

System.out.println("there was an array index out of bound");

private static double division(int a, int b)throws ArithmeticException{

return a/b;

7. Declare your own exception class that state raw marks cannot be greater than 100 in student grade
calculation.

Ans.

public class InvalidMark extends Exception{

public InvalidMark(){

System.out.println("the mark entered is invalid");

class Grade {

public static void calcgrade() throws InvalidMark {

public static void main(String[] args) {

try{

calcgrade();

}
catch(InvalidMark e){

System.out.println("the mark entered is invalid");

1)write a program that handle an exception that might be thrown by the


following code

public class Main


{
public static void main(String[] args)
{ Int[] a={1,2,0};
Try{
System.out.println(a[3]/a[1]);
}
catch(ArithmeticException ae)
{
Sytem.out.println(“An exception occurred:”+ae);
}
System.out.println(“”);
}
}

2)
Declare your own java exception class.use this exception class in another
class (hint:use the throws keyword)

public class MyOwnException extends Exception {


public MyOwnException(String msg){
super(msg);
}
}

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

Você também pode gostar