Java2-02Exceptions
Java2-02Exceptions
Handling
1
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.
22
EXCEPTIONS
3
Advantage of Exception Handling
statement 1;
statement 2;
statement 3;
statement 4;
statement 5; // Exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
4
• Suppose there are 10 statements in your program and there
occurs an exception at statement 5, the rest of the code will
not be executed i.e. statement 6 to 10 will not be executed.
5
Subclasses of The Exception
The Exception class has two main subclasses: IOException
class and RuntimeException Class.
6
Java Exception Categories
• Checked exceptions
• Unchecked exceptions
• Errors
7
Java Checked Exceptions
import java.io.File;
import java.io.FileReader;
9
Java Unchecked Exceptions
An unchecked exception is an exception that occurs at the
time of execution. These are also called as Runtime
Exceptions. These include programming bugs, such as logic
errors or improper use of an API. Runtime exceptions are
ignored at the time of compilation.
10
Unchecked Exception Example
11
Java Errors
12
Exception handlers
13
Java Exceptions keywords
1414
try...catch Example - 1
1515
try...catch Example - 2
try {
try-block
} catch(ExceptionClass1 e) {
catch-block
} catch(ExceptionClass2 e) {
catch-block
} finally {
finally-block
}
1717
use multiple try/catch statements
try {
file = new FileInputStream(fileName);
x = (byte) file.read();
} catch (IOException i) {
i.printStackTrace();
return -1;
} catch (FileNotFoundException f) // Not valid! {
f.printStackTrace();
return -1;
18
The Throws/Throw Keywords
19
Example - 1
import java.io.*;
public class className {
public void deposit(double amount) throws RemoteException
{
// Method implementation
throw new RemoteException();
}
// Remainder of class definition
}
20
Example - 2
import java.io.*;
public class className {
public void withdraw(double amount) throws
RemoteException, InsufficientFundsException {
// Method implementation
}
// Remainder of class definition
}
21
Throws Example
class Main { // declareing the type of exception public static
void findFile() throws IOException {
// code that may generate IOException
File newFile = new File("test.txt");
FileInputStream stream = new
FileInputStream(newFile);
}
public static void main(String[] args) {
try {
findFile();
} catch (IOException e) {
System.out.println(e);
}
}
} 2222
The throw keyword
24
What happens exception…
2525
Exceptions
26
Exception Propagation Example
class TestExceptionPropagation1{
void m(){ int data=50/0; }
void n(){ m(); }
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal ow...");
}
}
27
Exception Propagation Example
28
updating elements
public static void main(String[] args) throws IOException {
ArrayList<String> colors = new ArrayList<String>();
colors.add("While");
colors.add("Black");
colors.add("Yellow");
colors.add("Green");
colors.set(0, "Red");
for(String str:colors)
System.out.println(str);
}
29