0% found this document useful (0 votes)
6 views

Tutorial 08

This document is a tutorial for a BTech course on Object Oriented Programming Using Java, focusing on exception handling. It includes various code snippets that demonstrate exception handling concepts, such as try-catch blocks, and asks students to determine outputs and identify errors. The tutorial is designed to enhance understanding of Java's exception handling mechanisms.

Uploaded by

hirob28450
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Tutorial 08

This document is a tutorial for a BTech course on Object Oriented Programming Using Java, focusing on exception handling. It includes various code snippets that demonstrate exception handling concepts, such as try-catch blocks, and asks students to determine outputs and identify errors. The tutorial is designed to enhance understanding of Java's exception handling mechanisms.

Uploaded by

hirob28450
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

School of Computer Science Engineering and Technology

Course- BTech Type- Core


Course Code- CSET Course Name- Object Oriented Programming Using Java
Year- First Semester- Even Batch- BTech 2nd Semester

Tutorial-08

Tutorial No. Name CO1 CO2 CO3


08 Exception --  --
Handling

Objective: The main objective of this tutorial is to learn about the exception handling of Java
language.

8.1 What will be the Output of the below code:

public class TestException


{
public static void m1()
{
System.out.println("Bennett");
m2();
}
public static void m2()
{
System.out.println("University");
int i = 10/0;
System.out.println("Division successful");
}
public static void main(String[] args) {
m1();
}
}

8.2 Identify error in the following code?

public class Main


{
public static void main(String[] args) {

try{
School of Computer Science Engineering and Technology
System.out.println("Bennett");
int i = 10/0;
System.out.println("University");
}
catch(NullPointerException e)
{
System.out.println("Exception" + e.getMessage());
}
}
}

8.3 What is the result of the following code?

public class Main


{
public static void main(String[] args) {

try{
System.out.println("Bennett");
String s = null;

System.out.println(s.length());
}
catch(Exception e)
{
System.out.println("Exception" + e.getMessage());
}
catch(NullPointerException e)
{
System.out.println("Exception" + e.getMessage());
}
}
}

8.4 What is output of below code?

public class Main


{
public static void main(String[] args) {
try{
try{
System.out.println("Bennett");
String s = null;
School of Computer Science Engineering and Technology

System.out.println(s.length());
}
catch(NullPointerException e)
{
System.out.println("Exception: " + e.getMessage());
}
try{
int a[] = new int[5];

a[6] = 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception: " + e.getMessage());
}
}
catch(Exception e)
{

}
}
}

8.5 What is output of below code?

class Main{
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
finally{
System.out.println("finally block is always executed");

}
System.out.println("rest of the code...");

}
School of Computer Science Engineering and Technology
8.6 What is output of below code?

public class Main{


static void validate(int age){
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");

public static void main(String args[]){


validate(13);

System.out.println("rest of the code...");


}
}

8.7 What will be output of below code?

class VotingAgeException extends Exception{


public String getMessage()
{
return "Age exception";
}
}

public class Main{


static void validate(int age){
try{
if(age<18)
throw new VotingAgeException();
else
System.out.println("welcome to vote");
}
catch(VotingAgeException e)
{
System.out.println(e.getMessage());
}
}

public static void main(String args[]){


validate(13);
School of Computer Science Engineering and Technology
System.out.println("rest of the code...");
}
}

8.8 What is output of below code?

import java.io.IOException;

public class Main{

void m() throws IOException{

throw new IOException("device error");//checked exception


}

void n() throws IOException{


m();

void p(){

try{
n();

}catch(Exception e)
{
System.out.println("exception handled");

public static void main(String args[]){


Main obj=new Main();
obj.p();

System.out.println("normal flow...");

}
}

You might also like