Risky Behaviour — Chapter 11

Upulie Handalage
2 min readJul 25, 2022



No matter how good a programmer you are, you can’t control everything. When you write a risky method, you need code to handle the bad things that might happen. There is code that you can’t guarantee will work at runtime. Code that expects the file to be in the right directory, the server to be running, or the thread to stay asleep.

Few main points about Java exceptions include,

  1. an Exception is a type of a class that enables you to create objects ( from the inheritance tree)
  2. An exception unless called, is always thrown back to the caller
  3. The compiler checks for everything except RuntimeExceptions — The compiler guarantees: If you throw an exception in your code you must declare it using the throws keyword in your method declaration.
  4. If you call a method that throws an exception (in other words, a method that declares It throws an exception), you must acknowledge that you’re aware of the exception possibility. One way to satisfy the compiler is to wrap the call in a try/catch.
  5. A method can throw an exception when something fails at runtime.
  6. The compiler does NOT pay attention to exceptions that are of type RuntimeException. A RuntimeException does not have to be declared or wrapped in a try/catch (although you’re free todo either or both of those things)
  7. All Exceptions the compiler cares about are called ‘checked exceptions’
  8. Either handle or declare an exception.

try-catch block

A finally block is where you put code that must run regardless of an exception.

If the try block fails (an exception), flow control immediately moves to the catch block. When the catch block completes, the finally block runs.

When the finally block completes, the rest of the method continues on.

If the try block succeeds (no exception), flow control skips over the catch block and moves to the finally block.

When the finally block completes, the rest of the method continues on.

If the try or catch block has a return statement, finally will still run. Flow jumps to the finally, then back to the return.



Upulie Handalage

Everything in my point of view. Here for you to read on....