[!INDEX]
- Definition ( 3points )
- unhandled exception in caller method(file read write)
- Exception handling in caller method with try-catch (file read write) + no file
- Exception handling in caller method with try-catch + file.
- difference between throws and throw.(6 points, MRSBN, DCMSC)
1. Definition ( 3points )
[!NOTE]
- used in method declarations.
- specify that a method might throw certain exceptions during its execution.
- and that the calling method must handle or further propagate those exceptions.
- It is used primarily with checked exceptions (like
IOException,SQLException), which the compiler requires you to handle or declare.
2. unhandled exception in caller method(file read write)
package package2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main1 {
public static void main(String[] args) {
ReadAndWrite r1 = new ReadAndWrite();
r1.readFile(); // did not handle exception
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{ // caller is responsible
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile() throws FileNotFoundException { // caller is responsible
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
}
Unhandled exception type FileNotFoundExceptionJava(16777384)
3. Exception handling in caller method with try-catch (file read write) + no file
package package2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main1 {
public static void main(String[] args) {
ReadAndWrite r1 = new ReadAndWrite();
try {
r1.readFile();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("exception is handled");
}
}
}
class ReadAndWrite{
void readFile() throws FileNotFoundException{ // caller is responsible
String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream(filePath);
}
void saveFile() throws FileNotFoundException { // caller is responsible
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream(filePath1);
}
}
java.io.FileNotFoundException: C:\Users\rajat\Desktop\x\abc.java (The system cannot find the path specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:152)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:106)
at package2.ReadAndWrite.readFile(Main1.java:23)
at package2.Main1.main(Main1.java:11)
exception is handled
4. Exception handling in caller method with try-catch +file.
package package2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main1 {
public static void main(String[] args) {
ReadAndWrite r1 = new ReadAndWrite();
try {
r1.readFile();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("exception is handled");
}
try {
r1.saveFile();
System.out.println("file created");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ReadAndWrite{
//for read file
void readFile() throws FileNotFoundException{ // caller is responsible
// String filePath = System.getProperty("user.home") + "/Desktop/x/abc.java";
FileInputStream fis = new FileInputStream("d:/test.txt");
}
//for create file
void saveFile() throws FileNotFoundException { // caller is responsible
String filePath1 = System.getProperty("user.home") + "/Desktop/x/abc.java";
String text ="this is done";
FileOutputStream fos = new FileOutputStream("d:/test.txt");
}
}
file created
5. difference between throws and throw.(6 points, MRSBN, DCMSC)
| throw | throws |
|---|---|
| 1. Create e/x obj. Manually | Declare caller mt to handle the e/x |
| 2. mainly for r/t e/x | mainly for compile time e/x |
| 3. throw only single e/x | declare multiple e/x |
| 4. used within the method body | used with method signature |
| 5. followed by new instance | followed by class |
| 6. no statement after throw keyword and thus it can be used to break the statement.- |
No rule |
[!MA]
- throw-MRSBN, manually run the single body machine with new idea
- throws-DCMSC, declare, compile, multiple, signature, class
[!NOTE]
- void readFile() throws FileNotFoundException, NullPointerException, etc.
images