Today, a friend left me a message asking, what does try{...}catch(){...} mean? What is it used for?
Simply put, they are used to catch exceptions
Let's explain in detail with an example
try { Image img=Image.createImage("/image.png"); alert.setImage(img); }catch(java.io.IOException e) { System.out.println("Error"); }
Image img=Image.createImage("/image.png");
If the file cannot be found, an error will occur on it. To handle this exception error, use the above method
catch is to catch the exception and then handle the exception. Here is the output error
That is to say, it first executes the program in try brackets:
Image img=Image.createImage("/image.png");
alert.setImage(img);
If there is an error in it, jump directly to the catch block and throw System.out.println("Error"); This sentence tells you that there is an error in the try brackets.