[Personal memo] try-catch summary

2020/09/15 First post Updated on 2020/09/18

◎ Summary of methods you want to practice this time try method --- What you want to do catch method --- What to do if an error occurs while executing the try method finally method --- The block that must be executed at the end regardless of whether or not the catch method is executed.

◎ Contents of this article Create a new text file called "data.txt" in Explorer. Use FileWriter to write characters in the text file. Use the try-catch-finally method for detailed error handling Set the close method. (What is the close method? "Process to close the file" that is always written when the file is opened)

package trycatch;


import java.io.FileWriter;
import java.io.IOException;

public class trycatch1 {

	public void test() {

		FileWriter fw=null;
		/*If you write code that opens some file yourself in I / O processing,
Be sure to write the code that closes the file.=close method*/

		/*try close method-To write outside the catch method
		 *FileWriter is introduced here.
		 *Why write a close method outside the trycatch method?
		 * =If you find an error with the try method, you will immediately move to the catch method, so
		 *Because the close method may not be executed*/

		try {
			fw=new FileWriter("data.txt");
			/*here"data.txt"A new text file called
It will be created under this project.
You can see the file directly by opening the project in Window Explorer.
If you write to a file created in advance(Example)("c:\\nameOFfolder\\nameOFfile.txt")*/
			fw.write("hello");
			fw.write("Hero");
		    /*Even if it is executed, data.When there are no characters in txt
		     * close()The method may not be working properly.
		     * write()Even if you instruct the method to insert text
		     *The JVM actually writes it close()Before doing it. time lag*/

			fw.flush();
			//Forced write instructions

		}catch(IOException ioe){
			System.out.println("An entry / exit error has occurred");
			System.out.println(ioe);
		}catch(Exception e){
			System.out.println("Some kind of error has occurred");
			/*↑ Why write IOEception first here? = =
			 *In java, if multiple error handling is set,
Check in order from the top. If you handle one error
			try-Get out of the catch method.*/
			System.out.println(e);

		}finally{
			if(fw!=null) {
			//Why can ↑ be null? => Because I put null at the top of the class.
			try {
			fw.close();
			//Why if document? => If fw here becomes null, NullPointerException will occur, so write an if statement
			}catch(IOException ioe) {
				System.out.println("Some error occurred when closing the file");
			}
			/*◎ Why finally try in the block-Do you write a catch block?
			==>[Expected error when writing close method]If there is no countermeasure for, IOException will occur.
			*/
			}
		}

	}
	public static void main(String[] args) {

	}
}



◎ main method

package trycatch;

public class main {
	public static void main(String[] args) {
    //Instance generation
	trycatch1 t=new trycatch1();
  //Run
    t.test();
	}
}

Recommended Posts

[Personal memo] try-catch summary
[Memo] docker summary
Java static [Personal summary]
Personal summary about Java
docker command personal summary
[Personal memo] About Spring framework
Personal memo Progate Ruby I (2)
[Swift] Closure basics (personal memo)
Personal memo Eclipse plug-in installation
[Personal memo] Number guessing game
Personal memo Lombok's typical annotation
Personal memo Progate Ruby I (1)
Java HashMap, entrySet [Personal memo]
Personal memo: Metaprogramming with Java reflection
Summary
[Personal memo] Summary of stumbling blocks when deploying Rails apps to AWS
[Personal memo] Java data type is annoying
[Java] Personal summary of conditional statements (basic)
[Personal] JUnit5 memorandum memo (work in progress)
[Personal memo] Writing a file using BufferedWriter
[Personal memo] Java development environment is ready
[Personal memo] Reading a file using BufferedReader
Display message dialog in java (personal memo)