Java Exception Handling

java

https://www.javatpoint.com/exception-handling-in-java
https://dzone.com/articles/how-to-deal-with-exceptions

To get the stack traces back, you need to pass the option -XX:-OmitStackTraceInFastThrow to the JVM.

try {
  ...
} catch (Exception e) {
} finally {
}

try (BufferedReader br = new BufferedReader(new FileReader("FILE_PATH"))) {
  String line;
  while ((line = br.readLine()) != null) {
    System.out.println(line);
  }
} catch (IOException e) {
  e.printStackTrace();
}

// Getting the stack trace
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String exceptionStack = sw.toString();

import import org.apache.commons.lang.exception.ExceptionUtils;
String stackTrace = ExceptionUtils.getStackTrace(e);

When using a try, we must have at least one catch block or one finally block.
We cannot have a try block by itself.

class Error extends Throwable;
class Exception extends Throwable;
class RuntimeException extends Exception;

class CheckedException1 extends Exception {}
class UncheckedException extends RuntimeException {}

Error is used in situation when there is nothing a programmer can do about
an error.  For example, StackOverflow, OutOfMemoryError.

Exception is used when the programmer can handle the exception.

Uncheck exception: RuntimeException and classes that extend RuntimeException
are called unchecked exceptions.

Checked exception: Other exception classes that do not fall under the 
RuntimeException are called checked exception.

RuntimeException is an unchecked exception.  The calling method is not expected
to handle this exception.  In other words, if I throw a RuntimeException, I do
not want the caller method to handle the exception.  I just want the program
to terminate.

If I want the caller method to know about the exception, then I would throw
a checked exception (one that does not inherit from RuntimeException).

To create a custom exception, create an empty class that extends the Exception class:
class CurrenciesDoNotMatchException extends Exception {
}

To create a custom unchecked exception, create an empty class that extends the 
RuntimeException class:
class CurrenciesDoNotMatchException extends RuntimeException {
}

To handle multiple exceptions in one catch block (as of JEE7):
try {
  ...
} catch (IOException | SQLException ex) {
  ...
}

// Try with resources

With the 'try with resources' construct, when the try block ends, the resources
are automatically released.  We do not need to create a separate finally block.

try (BufferedReader br = new BufferedReader(new FileReader("FILE_PATH"))) {
  String line;
  while ((line = br.readLine()) != null) {
    System.out.println(line);
  }
} catch (IOException e) {
  e.printStackTrace();
}

The 'try with resources' construct is available to any class that implements the 
AutoCloseable interface.  In the above example, BufferedReader implements 
AutoCloseable interface.

public interface AutoCloseable {
  void close() throws Exception;
}

How to implement a global exception handler?

See the java-articles page for the link.

How can we get the stack trace as a string?

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String exceptionStack = sw.toString();

I am not sure if we can use e.toString().

If we are using import org.apache.commons.lang.exception.ExceptionUtils:

import import org.apache.commons.lang.exception.ExceptionUtils;
String stackTrace = ExceptionUtils.getStackTrace(e);

What is the cause for Java throwing exception without stacktrace?

  1. You are probably using the Sun JVM, which performs a lot of optimization. To get the stack traces back, you need to pass the option -XX:-OmitStackTraceInFastThrow to the JVM.
  2. If we are using JRE, try to switch to using JDK.
  3. When compiling our classes, make sure that we specify debug="true" in our javac command (in out ant build.xml file)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License