Handling Expt .Discussion 1
Handling Expt .Discussion 1
1. try
A try block consists of all the doubtful statements that can throw
exceptions.
A try block cannot be executed on itself; it requires at least
one catch block or finally block.
If an exception occurs, the control flows from the try-to-
catch block.
When an exception occurs in a try block, the appropriate
exception object is redirected to the catch block.
This catch block handles the exception according to its
statements and continues the execution.
Syntax
try
{
//Doubtful Statements.
}
2. catch
The catch block handles the exception raised in the try block.
The catch block or blocks follow every try block.
The catch block catches the thrown exception as its parameter
and executes the statements inside it.
The declared exception must be the parent class exception,
the generated exception type in the exception class hierarchy,
or a user-defined exception.
Syntax
try
{
// code
}
catch(Exception e)
{
// code to handle exceptions
}
1.
2. class Main
3. {
4. public static void main(String[] args)
5. {
6. try
7. {
8. // code that generate exception
9. int divideByZero = 7 / 0;
10. System.out.println("Rest of code in try block");
11. }
12. catch (ArithmeticException e) {
13. System.out.println("ArithmeticException => " + e.getMessage());
14. }
15. }
}
Output
ArithmeticException => / by zero
3. throw
The throw keyword is used to explicitly throw a checked or
an unchecked exception.
The exception that is thrown needs to be of type Throwable or a
subclass of Throwable.
We can also define our own set of conditions for which we can
throw an exception explicitly using the throw keyword.
The program's execution flow stops immediately after the throw
statement is executed, and the nearest try block is checked to
see if it has a catch statement that matches the type of
exception.
Syntax
class ThrowExample {
// Method to check if a number is negative
public static void checkNumber(int number) {
if (number < 0) {
// Throwing an IllegalArgumentException if the number is negative
throw new IllegalArgumentException("Number cannot be negative");
} else {
System.out.println("Number is " + number);
}
}
Output
Caught an exception: Number cannot be negative
4. finally
The finally block in Java always executes even if there are
no exceptions. This is an optional block. It is used to execute
important statements such as closing statements, releasing resources,
and releasing memory. There could be one final block for every try
block. This finally block executes after the try...catch block.
Syntax
try
{
//code
}
catch (ExceptionType1 e1)
{
// catch block
}
finally
{
// finally block always executes
}
class Main
{
public static void main(String[] args)
{
try
{
// code that generates exception
int divideByZero = 5 / 0;
}
catch (ArithmeticException e)
{
System.out.println("ArithmeticException => " + e.getMessage());
}
finally
{
System.out.println("This is the finally block");
}
}
}
Run Code >>
In this Java example, trying to divide by zero results in
an ArithmeticException that is caught and accompanied by an error
message. The "finally" block also always runs, printing "This is the
finally block" whether or not an exception was raised.
Output
ArithmeticException => / by zero
This is the finally block
References
https://www.scholarhat.com/tutorial/java/exception-handling-in-java