An exception is an error that disrupts the flow of the program. So, when the JVM sees an error, it will throw an exception object which contains the details of the error found. It will undergo a certain process until it terminates the program.
Three exception handler components:
1. try
2. catch
3. finally
==================
try {
// ... try block
}catch(Exception name){
//... catch block
}finally {
//... finally block
}
=================
In the try block is where you put your code that might throw an exception. A program where you anticipate an exception. You can add a parameter in the try if you want your closeable program to automatically close it. Like
try(create your object here ){
//... try block
}catch(Exception1 name){
// ... catch block
}catch(Exception2 name){
// ...catch block
}finally {
// ...finally block
}
In the catch block, you can put one or more catch block after the try block. In the catch parameter is where you specify the type of exception you want to catch. Meaning, you want to catch the class name of an exception which is a predefined code that was program as type of such an error like IndexOutOfBoundsException, IOException and so on. So, if the code in the try block throws an IndexOutOfBoundsException object, then your catch block must have the same type to catch it. And the catch block body will run. You usually prints out the error here in the catch block but you can also do some error recovery, prompt the user to make a decision or do some chained exceptions to pass it to a better handler. Note that you can pass more than one exception in the catch parameter which makes the catch block as implicitly final but the problem is you cannot assign new values inside its catch block.
In the finally block, this block always executes no matter what happens in the try block. After the try block the finally block will execute. So, normally, this is where you put all those objects that needs to be close. Like for example the object of a file reader that you created in the try block, you can use the finally block to close it.
=================
class YourFileClass{
public static void main(String[] args) throws IOException{
String myFile = "MyTemporaryFile.txt";
YourFileClass.writeSomething(myFile);
YourFileClass.readSomething(myFile);
}
static void writeSomething(String fileName){
PrintWriter printWriter = null;
try{
printWriter = new PrintWriter(new FileWriter(fileName));
Scanner scan = new Scanner(System.in);
while(true){
String message = scan.next();
printWriter.write(message + " ");
if(message = "end"){
break;
}
}
scan.close();
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
if(printWriter != null){
printWriter.close();
}else {
System.out.println("PrintWriter is null");
}
}
}
static void readSomethig(String fileName) throws IOException{
BufferedReader bufferedReader = null;
try{
bufferedReader = new BufferedReader(new FileReader(filname));
String messageLine = bufferedReader.readline();
while(messageLine != null){
System.out.println(messageLine);
messageLine = bufferedReader.readline();
}
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
if(bufferedReader != null){
bufferedReader.close()
}else {
System.out.println("Buffered reader is null");
}
}
}
}
=================
Result:
This is just a test for reading and writing
in a file. end
This is just a test for reading and writing in a file. end
Note:
1. A text file will be created in one of the folder. And the one in italic and underline words will be encoded in there. Then at the bottom portion of the underlined words is the text read from that text file.
2. There is a throws IOException in the readSomething() method. I did use that because it is required by the program to add it even I included a catch IOException. You use a throw if you want the super class to handle the exception for you and wanted to catch it.
YOU ARE READING
Java Programming
Rastgelehttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...