Skip to content

Instantly share code, notes, and snippets.

View benweidig's full-sized avatar

Ben Weidig benweidig

View GitHub Profile
public interface AutoCloseable {
void close() throws Exception;
}
public interface Closeable extends AutoCloseable {
void close() throws IOException;
}
interface MyInterface {
void doWork() throws IOException;
}
class MyClass {
public void moreWork() throws FileNotFoundException {
...
public void logMessage(String message) {
if (message == null) {
throw new NullPointerException("'message' must not be null'");
}
if (message.isEmpty()) {
throw new IllegalArgumentException("'message' must not be empty");
}
...
try (Socket socket = new Socket();
InputStream input = new DataInputStream(socket.getInputStream());
OutputStream output = new DataOutputStream(socket.getOutputStream())) {
...
}
try (FileReader reader = new FileReader("awesome-file.txt")) {
int content = 0;
while (content != -1) {
content = reader.read();
...
}
}
catch (IOException e) {
// handle any exceptions
}
FileReader reader = null;
try {
reader = new FileReader("awesome-file.txt");
int content = 0;
while (content != -1) {
content = reader.read();
...
}
}
try {
...
}
catch (FileNotFound | UnsupportedEncodingException e) {
...
}
try {
// open a file
// read file
// do the work
}
catch(FileNotFoundException e) {
// handle missing file
}
public boolean isDeleted() {
return getTimeDeleted() != null;
}
long primitiveValue = 1_024L;
Long objectValue = 1_024L;
long primitiveSum = primitiveValue + objectValue;
Long objectSum = primitiveValue + objectValue;