0% found this document useful (0 votes)
6 views3 pages

UEC2023123 Assignment11

The document contains Java code examples demonstrating exception handling, including the use of try-catch blocks and finally clauses. It highlights common exceptions such as ArithmeticException and ArrayIndexOutOfBoundsException, along with their handling. The code also illustrates the importance of the order of catch blocks and the behavior of finally blocks in exception scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

UEC2023123 Assignment11

The document contains Java code examples demonstrating exception handling, including the use of try-catch blocks and finally clauses. It highlights common exceptions such as ArithmeticException and ArrayIndexOutOfBoundsException, along with their handling. The code also illustrates the importance of the order of catch blocks and the behavior of finally blocks in exception scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 11

Roll No: UEC2023123


Name: Gresee Dhok
Batch A2

Program to incorporate Exception Handling.

class SuperSubCatch {
public static void main (String args []) {
try {
int a = 0;
int b = 42/a;
}
catch (Exception e) {
System.out.println ("Generic Exception catch.");
}
catch (ArithmeticException e) {
System.out.println ("This is never reached.");
}
}
}
SuperSubCatch.java:10: error: exception ArithmeticException has already been caught
catch (ArithmeticException e) {
^

class MultiCatch {
public static void main (String args []) {
try {
int a = args.length;
System.out.println ("a" = +a);
int b = 42/a;
int c[] = {1};
c[42] = 99;
}

catch (ArithmeticException e) {
System.out.println ("Divide by 0: " +e);
}
catch (ArrayIndexOfBoundsException e) {
System.out.println ("Array Index oob: " +e);
}
System.out.println ("After catch try block");
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac MultiCatch.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java MultiCatch
a=0
Divide by 0: java.lang.ArithmeticException: / by zero
After catch try block
class NesTry {
public static void main (String args []) {

try {
int a = args.length;
int b = 42/a;
System.out.println ("a " + a);

try {
if (a == 1) a = a/a-a;
if (a == 2) {
int c[] = {1};
c[42] = 91;
}
}

catch (ArrayIndexOutOfBoundsException e){


System.out.println (e);
}
}

catch (ArithmeticException e) {
System.out.println (e);
}
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac NesTry.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java NesTry
java.lang.ArithmeticException: / by zero

class FinallyDemo {
static void procA () {
try {
System.out.println ("Inside procA.");
throw new RuntimeException ("demo");
}
finally {
System.out.println ("prosA's finally.");
}

static void procB () {


try {
System.out.println ("Inside procB.");
return;
}
finally {
System.out.println ("prosB's finally.");
}
}

static void procC () {


try {
System.out.println ("Inside procA.");
}
finally {
System.out.println ("prosC's finally.");
}

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

try {
procA ();
}
catch (Exception e){
System.out.println ("Exception Caught.");
}

procB ();
procC ();

}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac FinallyDemo.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java FinallyDemo
Inside procA.
prosA's finally.
Exception Caught.
Inside procB.
prosB's finally.
Inside procA.
prosC's finally.

You might also like