Try Catch in Java: Exception Handling Example
What is Exception?
Exception is an event that interrupts the normal flow of execution. It is a disruption during the execution of the Java program.
In this tutorial, you will learn-
Syntax Errors Example:
Instead of declaring
int a; you mistakenly declared it as
in a; for which compiler will throw an error.
Example: You have declared a variable
int a; and after some lines of code you again declare an integer as
int a;. All these errors are highlighted when you compile the code.
Runtime Errors Example
A Runtime error is called an Exceptions error. It is any event that interrupts the normal flow of program execution.
Example for exceptions are, arithmetic exception, Nullpointer exception, Divide by zero exception, etc.
Exceptions in Java are something that is out of developers control.
How to Handle Exception
So far we have seen, exception is beyond developer’s control. But blaming your code failure on environmental issues is not a solution. You need a Robust Programming, which takes care of exceptional situations. Such code is known as Exception Handler.
In our example, good exception handling would be, when the server is down, connect to the backup server.
To implement this, enter your code to connect to the server (Using traditional if and else conditions).
You will check if the server is down. If yes, write the code to connect to the backup server.
Such organization of code, using “if” and “else” loop is not effective when your code has multiple java exceptions to handle.
In our example, TRY block will contain the code to connect to the server. CATCH block will contain the code to connect to the backup server.
In case the server is up, the code in the CATCH block will be ignored. In case the server is down, an exception is raised, and the code in catch block will be executed.
So, this is how the exception is handled in Java.
Syntax for using try & catch
- All exception classes in Java extend the class ‘Throwable’. Throwable has two subclasses, Error and Exception
- The Error class defines the exception or the problems that are not expected to occur under normal circumstances by our program, example Memory error, Hardware error, JVM error, etc
- The Exception class represents the exceptions that can be handled by our program, and our program can be recovered from this exception using try and catch block
- A Runtime exception is a sub-class of the exception class. The Exception of these type represents exception that occur at the run time and which cannot be tracked at the compile time. An excellent example of same is divide by zero exception, or null pointer exception, etc
- IO exception is generated during input and output operations
- Interrupted exceptions in Java, is generated during multiple threading.
Example: To understand nesting of try and catch blocks
Step 1) Copy the following code into an editor.
Step 6) Compilation Error? This is because Exception is the base class of ArithmeticException Exception. Any Exception that is raised by ArithmeticException can be handled by Exception class as well.So the catch block of ArithmeticException will never get a chance to be executed which makes it redundant. Hence the compilation error.
- An Exception is a run-time error which interrupts the normal flow of program execution.Disruption during the execution of the program is referred as error or exception.
- Errors are classified into two categories
- Compile time errors – Syntax errors, Semantic errors
- Runtime errors- Exception
- A robust program should handle all exceptions and continue with its normal flow of program execution. Java provides an inbuilt exceptional handling method
- Exception Handler is a set of code that handles an exception. Exceptions can be handled in Java using try & catch.
- Try block: Normal code goes on this block.
- Catch block: If there is error in normal code, then it will go into this block