0% found this document useful (0 votes)
24 views12 pages

Java Assignment 2

The document contains the answers to 8 questions about Java exception handling programs. It includes programs demonstrating exception handling without and with try/catch blocks, different types of exceptions (checked and unchecked), multiple catch blocks, finally blocks, user-defined exceptions, and throwing built-in exceptions. The last program shows using throws to declare that a method can throw an exception.

Uploaded by

Vivek Rehal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
24 views12 pages

Java Assignment 2

The document contains the answers to 8 questions about Java exception handling programs. It includes programs demonstrating exception handling without and with try/catch blocks, different types of exceptions (checked and unchecked), multiple catch blocks, finally blocks, user-defined exceptions, and throwing built-in exceptions. The last program shows using throws to declare that a method can throw an exception.

Uploaded by

Vivek Rehal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

JAVA ASSIGNMENT

Name: Aastha Bali


Roll Number: 2110992705
BCA 4C
Q1) Program without exception handling

Answer) Program:

public class Java_1 {

public static void main(String[] args) {

System.out.println(“Name: Aastha Bali”);

System.out.println(“Roll NO: 2110992705”);

int a = 10;

int b = 0;

System.out.println(a/b);

}}

Q2) Program with exception handling

Answer) Program:
public class Java_2 {

public static void main(String[] args) {

try{

int a = 12;

int b = 0;

System.out.println(a/b);

catch(Exception e){

System.out.print("Exception is: " + e);

}}}

Q3) Program to demonstrate different types of Exceptions

Answer) There are broadly two kinds of exceptions

Exception

Checked exceptions Unchecked exceptions


1. Checked exceptions: These are the exceptions that are checked by the compiler at compile
time. If a method throws a checked exception, then the caller of the method must either handle
the exception or declare it in the throws clause.

Some examples of checked exceptions are as follows:

 IOException – This exception is raised when an input/output operation falls.


 SQLException – This exception is raised when a database operation fails.
 ClassNotFoundException – This exception is raised when a class cannot be found.
 Instantiation Exception – This exception is raised when an object cannot be
instantiated.
 NoSuchMethodException – This exception is raised when a method cannot be found.

2. Unchecked exceptions: These are the exceptions that are not checked by the compiler at
compile time. They include runtime exceptions and errors.

Some examples of unchecked exception are as follows:

 ArithmeticException – This exception is raised when an arithmetic operation fails.

Program:

public class Java_1 {

public static void main(String[] args) {

int a = 10;

int b = 0;

System.out.println(a/b);

}}

 ArrayIndexOutOfBoundsException – This exception is raised when an array index is out


of bounds.
Program:

public class Array_Index_Out_of_Bounds_Exception {

public static void main(String[] args) {

System.out.println("Name: Aastha Bali");

System.out.println("Roll NO: 2110992705");

int arr[] = new int[3];

arr[0] = 10; arr[1] = 20; arr[3] = 30; arr[4] = 45;

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

}}

 StringIndexOutOfBoundsException – It is thrown by String class methods to indicate


that an index is either negative than the size of the string.
Program:

public class String_Index_Out_Of_Bounds_Exception {

public static void main(String[] args) {

String name = "Aastha Bali";

System.out.println("Length of string name is: " + name.length());

char a = name.charAt(16);

}}
 NumberFormatException – This exception is raised when a method could not convert a
string into a numeric format.

Program:

public class Number_Format_Exception {

public static void main(String[] args) {

System.out.println("Name: Aastha Bali");

System.out.println("Roll NO: 2110992705");

int integer = Integer.parseInt("Number format exception");

System.out.println(integer);

}}

 NullPointerException – This exception is raised when a null value is used where an


object is required.
Program:

public class Null_Pointer_Exception {

public static void main(String[] args) {

String str = null;

System.out.println(str.toUpperCase());

}}

Q4) Program to demonstrate Multiple Catch Blocks

Answer) Program:

public class Multiple_Catch_Block {

public static void main(String[] args) {

try{

int arr1[] = new int[5];

arr1[10] = 10;

int i = 23;

int j = 0;

int k = i/j;

System.out.println("k");

catch(ArithmeticException e){
System.out.println(e);

catch(ArrayIndexOutOfBoundsException e){

System.out.println(e);

catch(NullPointerException e){

System.out.println(e);

catch(Exception e){ // this block will always be written in the end just before the finally
block

System.out.println(e);

// catch blocks can also be written like this

// catch(ArithmeticException | ArrayIndexOutOfBoundsException | //
StringIndexOutOfBoundsException e){

// System.out.println("Error");

// }

finally{

System.out.println("Finally Block");

}}}

Q5) Program to demonstrate Finally Block

Answer) Program:

public class Finally_Block{

public static void main(String[] args) {

try{
int a = 3;

int b = 0;

int c = a/b;

System.out.println(c);

catch(Exception e){

System.out.println(e);

finally{

System.out.println("Finally Block");

}}}

Q6) Program to demonstrate Throw user defined Exception

Answer) Program:

class InvalidAgeException extends Exception{

InvalidAgeException(String msg){

System.out.println(msg);

}}

public class User_Defined_Exception {

public static void main(String[] args) {


try{

vote(12);

catch(Exception e){

System.out.println("Exception");

}}

// throw user defined exception

public static void vote(int age) throws InvalidAgeException{

if(age < 18){

throw new InvalidAgeException("Not eligible to vote");

else{

System.out.println("Eligible for voting");

}}}

Q7) Program to demonstrate Throw with Built in Exception

Answer) Program:

public class Built_In_Exception {

public static void main(String[] args) {


try{

int arr[] = new int[4];

arr[0] = 10;

arr[1] = 20;

arr[2] = 30;

arr[3] = 40;

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

catch(Exception e){

System.out.println("Exception is: " + e);

throw new ArrayIndexOutOfBoundsException("Array Index Out of Bounds Exception");

}}

Q8) Program to demonstrate throws

Answer) Program:

import java.io.*;

import java.lang.Thread;
class HelloWorld {

public static void main(String[] args) {

System.out.println("Name: Aastha bali ");

System.out.println("Roll No: 2110992705");

try {

for (int i = 0; i < 10; i++) {

Thread.sleep(1000);

System.out.println(i); }

catch (Exception e) {

System.out.println(e);

}}
Thank you so much

You might also like