0% found this document useful (0 votes)
20 views2 pages

Exception

Important questions

Uploaded by

krakshit2006
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)
20 views2 pages

Exception

Important questions

Uploaded by

krakshit2006
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/ 2

INFORMATION TECHNOLOGY

CLASS-12
Exception Handling
1. Define exception.
An exception is an unwanted or unexpected event, which occurs during the execution of a
program i.e at run time, that disrupts the normal flow of the program’s instructions.
For example: an array index reference was out of range, or an attempt was made to divide an
integer by zero.
2. How exceptions can be handled in Java?

Java provides the following keywords to handle an exception:

try - A try block surrounds the part of the code that can generate exception(s).

catch – The catch blocks follow a try block. A catch block contains the exception handler -
specific code that is executed when the exception occurs. Multiple catch blocks following a try
block can handle different types of exceptions.
try {

// Part of the program where an exception might occur

catch (exceptiontype1 argument1) {

// Handle exception of the exceptiontype1 }

catch (exceptiontype2 argument2) {

// Handle exception of the exceptiontype2

finally {

//Code to be executed when the try block exits

}
3. Give an example of exception handling.

public class Main {


public static void main(String[ ] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}

4. Find the output of :


public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}
The output will be:

Something went wrong.


The 'try catch' is finished.

You might also like