Tutorial by Examples

An exception can be caught and handled using the try...catch statement. (In fact try statements take other forms, as described in other examples about try...catch...finally and try-with-resources.) Try-catch with one catch block The most simple form looks like this: try { doSomething(); } ...
The following example shows the basics of throwing an exception: public void checkNumber(int number) throws IllegalArgumentException { if (number < 0) { throw new IllegalArgumentException("Number must be positive: " + number); } } The exception is thrown on the 3...
Under most circumstances, it is simpler from a code-design standpoint to use existing generic Exception classes when throwing exceptions. This is especially true if you only need the exception to carry a simple error message. In that case, RuntimeException is usually preferred, since it is not a che...
Java SE 7 As the try-catch-final statement example illustrates, resource cleanup using a finally clause requires a significant amount of "boiler-plate" code to implement the edge-cases correctly. Java 7 provides a much simpler way to deal with this problem in the form of the try-with-res...
When an exception object is created (i.e. when you new it), the Throwable constructor captures information about the context in which the exception was created. Later on, this information can be output in the form of a stacktrace, which can be used to help diagnose the problem that caused the excep...
InterruptedException is a confusing beast - it shows up in seemingly innocuous methods like Thread.sleep(), but handling it incorrectly leads to hard-to-manage code that behaves poorly in concurrent environments. At its most basic, if an InterruptedException is caught it means someone, somewhere, c...
All Java exceptions are instances of classes in the Exception class hierarchy. This can be represented as follows: java.lang.Throwable - This is the base class for all exception classes. Its methods and constructors implement a range of functionality common to all exceptions. java.lang.Excep...
Exceptions are errors which occur when a program is executing. Consider the Java program below which divides two integers. class Division { public static void main(String[] args) { int a, b, result; Scanner input = new Scanner(System.in); System.out.println(&qu...
Although it's bad practice, it's possible to add multiple return statements in a exception handling block: public static int returnTest(int number){ try{ if(number%2 == 0) throw new Exception("Exception thrown"); else return x; } catch(Exception e){ ...
This example covers some advanced features and use-cases for Exceptions. Examining the callstack programmatically Java SE 1.4 The primary use of exception stacktraces is to provide information about an application error and its context so that the programmer can diagnose and fix the problem. Som...
The try...catch...finally statement combines exception handling with clean-up code. The finally block contains code that will be executed in all circumstances. This makes them suitable for resource management, and other kinds of cleanup. Try-finally Here is an example of the simpler (try...finally...
Java's checked exception mechanism requires the programmer to declare that certain methods could throw specifed checked exceptions. This is done using the throws clause. For example: public class OddNumberException extends Exception { // a checked exception } public void checkEven(int number) ...

Page 1 of 1