import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TryBasic {
public static void main(String[] args) {
try {
var in = new FileInputStream("C:/data/nothing.gif");
var data = -1;
while ((data = in.read()) != -1) {
System.out.printf("%02X ", data);
}
//Throw a FileNotFoundException exception in the FileInputStream class
} catch (FileNotFoundException e) {
System.out.println("The file was not found.");
} catch (IOException e) {
//Access the exception object through the specified exception variable (e)
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TryFinally {
public static void main(String[] args) {
FileInputStream in = null;
try {
in = new FileInputStream("C:/data/nothing.gif");
var data = -1;
while ((data = in.read()) != -1) {
System.out.printf("%02X ", data);
}
} catch (FileNotFoundException e) {
System.out.println("The file was not found.");
} catch (IOException e) {
e.printStackTrace();
} finally {
//File close with or without exception
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
var input = new FileInputStream(...)
* try(input){...}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TryWithResources {
//Resource declaration at the beginning of the block
public static void main(String[] args) {
try (var in = new FileInputStream("C:/data/hogee.gif")) {
var data = -1;
while ((data = in.read()) != -1) {
System.out.printf("%02X ", data);
}
//Automatic resource release when exiting the try block
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch (Exception e)
catches all exceptionsException 1 | Exception 2
(in either case)//Multi catch
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class TryMulti {
public static void main(String[] args) {
try {
var in = new FileInputStream("C:/data/nasi.gif");
var data = -1;
while ((data = in.read()) != -1) {
System.out.printf("%02X ", data);
}
var uri = new URI( "https://www.neko.example.com");
System.out.println(uri.toString());
//Multi catch
} catch (IOException | URISyntaxException e) {
System.out.println("The file cannot be accessed.");
e.printStackTrace();
}
}
}
public FileInputStream(File file) throws FileNotFoundException{
//Abbreviation
-ea
option is specified in the java commandpublic class AssertBasic {
private static double getTrapezoidArea(double upper, double lower, double height) {
//Exception occurs when argument is 0 or less
assert upper > 0 && lower > 0 && height > 0;
return (upper + lower) * height / 2;
}
public static void main(String[] args) {
System.out.println(AssertBasic.getTrapezoidArea(-2, 4, 0));
}
}
public class TryRethrow {
//MySampleException,Rethrow method may occur in MyLibException
public static void rethrow(boolean flag) throws MySampleException, MyLibException {
try {
if (flag) {
throw new MySampleException();
} else {
throw new MyLibException();
}
//Received as Exception type and thrown again as it is
} catch (Exception e) {
throw e;
}
}
}
getCause
method **
var ex = e.getCause();
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Paths;
//You only need to be aware of MySampleException as a translation exception
public class UseTrans {
public void readHttpPages() throws MySampleException {
try (var reader = Files.newBufferedReader(Paths.get("C:/data/link.txt"))) {
var line = "";
while ((line = reader.readLine()) != null) {
var client = HttpClient.newHttpClient();
var req = HttpRequest.newBuilder().uri(URI.create(line)).build();
var res = client.send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(res.body());
}
} catch (IOException | InterruptedException e) {
throw new MySampleException(e);
}
}
}
//Inherit Exception (derived class)
public class MySampleException extends Exception {
//Override constructor
public MySampleException() {
super();
}
public MySampleException(String message) {
super(message);
}
public MySampleException(String message, Throwable cause) {
super(message, cause);
}
public MySampleException(Throwable cause) {
super(cause);
}
}
Recommended Posts