0% found this document useful (0 votes)
8 views9 pages

Lecture 9

Chapter Nine discusses exception handling in Java, explaining how errors can occur during code execution and how Java throws exceptions. It introduces the try and catch statements to handle errors gracefully, providing examples of their usage. Additionally, it covers the finally statement, which allows code execution after try and catch blocks, regardless of whether an exception occurred.

Uploaded by

aiaareen2
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)
8 views9 pages

Lecture 9

Chapter Nine discusses exception handling in Java, explaining how errors can occur during code execution and how Java throws exceptions. It introduces the try and catch statements to handle errors gracefully, providing examples of their usage. Additionally, it covers the finally statement, which allows code execution after try and catch blocks, regardless of whether an exception occurred.

Uploaded by

aiaareen2
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/ 9

Chapter Nine

Exception handling

By:Karwan Mohammed
Java Exceptions
• When executing Java code, different errors can occur:
• coding errors made by the programmer
• errors due to wrong input
• or other unforeseeable things.
• When an error occurs, Java will normally stop and generate an error message.
• The technical term for this is: Java will throw an exception (throw an error).
• The try statement allows you to define a block of code to
be tested for errors while it is being executed.
Java try and catch • The catch statement allows you to define a block of code
to be executed, if an error occurs in the try block.
• The try and catch keywords come in pairs:
Consider the following
example:

public class Main {


public static void main(String[] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
}
}
If an error occurs, we can
use try...catch to catch the public class JavaApplication22 {
error and execute some public static void main(String[] args) {
try {
code to handle it: int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
}
}
Example: try…catch with
import java.util.Scanner;
scanner public class JavaApplication22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter your age");
try {
int x = sc.nextInt();
System.out.println("your age is :" + x);
} catch (Exception e) {
System.out.println("please enter a number");
}
}
}
}
Example: different exception public class JavaApplication22 {
types public static void main(String[] args) {
try {
// int [] x = null;
int[] x = {4};
//System.out.println(x[0]);
System.out.println(x[2]);
} catch (NullPointerException e) {
System.out.println("your array index was null");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("your array in dext is out of
boundary");
}
}
}
public class JavaApplication22 {
Finally public static void main(String[] args) {
try {
The finally statement lets you int[] myNumbers = {1, 2, 3};
execute code, after try...catch, System.out.println(myNumbers[10]);
regardless of the result: } catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}
Thank you

Any questions?

You might also like