Exception handling In Java.

Gawesh Prabhashwara
3 min readMay 4, 2022

Head First Java Edition ii Chapter xi,

*No matter how talented a programmer you are, you can’t control everything. It is possible for things to go wrong at any time. Rather than bugs, there will be some hazards during runtime, such as not knowing if it will execute or become stuck. As a result, in this chapter, we’ll look at how to deal with exceptions.

*The Java exception-handling system is a clean, well-lit technique for dealing with “extraordinary situations” that arise during runtime; it allows you to concentrate all of your error-handling code in one place. It’s predicated on you realizing that the function you’re calling is potentially dangerous (i.e., it could throw an exception), so you can create code to handle it. If you know you’ll get an exception when calling a specific method, you may plan for it and possibly even recover from the issue that produced the exception.

TRY AND CATCH BLOCK,

*The compiler needs to know that you’re aware that you’re calling a potentially dangerous method. The compiler will relax if you enclose the dangerous code in a try/catch block.

*A try/catch block informs the compiler that you’re aware that anything unusual might occur in the method you’re calling and that you’re prepared to handle it.

*An object is what you capture, and an exception is also an object. In the following code, the catch argument is declared as type Exception, and the parameter reference variable is ex.

Whose code throws it,

*If your code is the one that catches the exception, who is the one that throws it? You now have knowledge of this.

*When calling a method that throws an exception, remember to catch and declare the exception.

*Toss is used to create a new exception and throw it in this case.

*throws: a keyword that is used to indicate that an exception has occurred.

How to work try and catch,

*When you use the dangerous method. What is the best way to try to catch?

  1. The risky method either succeeds, and the try block completes and catch block is not executed.
  2. The risky method doesn’t succeed (try blocks will fail) ,the rest of the code in the try block will not be executed and the catch block will catch the exception.

Finally block,

*Finally, code that must run regardless of an exception is placed in a block.

*Finally will still run if the try or catch block has a return statement.

*Let’s look at an example.

*Without finally, you must include turnOvenOff() in both the attempt and the catch because the oven must be turned off regardless.

Error,

*An error is a term for an unchecked exception. It should not, however, be handled or declared in any way. The error arises when something unexpected happens in the program’s environment.

Example of errors:

  • StackOverFlowError :- When a method is called itself too many times.
  • ExceptionInInitializerError :- thrown if an exception occurs at the initializing process like a static block.

Exception rules,

  • you cannot have a catch or finally without a try
  • You cannot put code between the try and the catch
  • try MUST be followed by either a catch or a finally
  • A try with only a finally (no catch) must still declare the exception.

References,

[1]Sierra, Kathy, and Bert Bates. “Head First Java™ Second Edition.” (2021)

--

--