Flat Preloader Icon

Exception Handling

Overview

  • What is exception?
  • Constructs to deal with exceptions
  • Classification of exceptions
  • Throwing exceptions to the caller
  • Creating your own exception class.
  • Analyzing the stack trace

Exception handling in Java is a mechanism that allows you to gracefully handle unexpected or exceptional situations that may occur during the execution of a program. Exceptions can occur due to a wide range of reasons, such as input errors, network issues, file access problems, and more. Java provides a robust exception-handling mechanism to deal with such situations effectively. Here are the key aspects of exception handling in Java

Core Java Statements

If Statement

  • Use if to specify a block of code to be executed, if a specified condition is true
				
					•	The syntax of if statement is 
either one of these two:

If (booleanExpression)
{
statement(s)
}
Or
If (booleanExpression)
{
 

}
Else
{

}

				
			

While Statement

  • The while loop loops through a block of code as long as a specified condition is true:
				
					•	The syntax of while statement is:

While (booleanExpression)
{
statement(s)
}

				
			

Do-while Statement

  • The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
				
					•	The syntax of do-while statement is:

Do {
statement(s)
}
While (booleanExpression);

				
			

For Statement

    • When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
				
					•	The for statement has following syntax:
For (init; booleanExpression; update)
{
statement (s)
}

				
			

Break Statement

    • You have already seen the break statement used in an earlier chapter of this tutorial. It was used to “jump out” of a switch statement.
				
					•	The Break Statement is used to break from
an enclosing
for, while, do and switch statement.

•	Cannot used elsewhere as we get 
a compilation error.

•	E.g.
for (int i=0; i < 10; i++){ if(i==6)
{
break;
}
System.out.println(i);
}

				
			

Continue Statement

    • The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
				
					•	The Continue Statement is like Break, 
but it stops only
the execution of current statement
and causes control to return to next iteration.

•	E.g.
for (int i=0; i < 10; i++){ if(i==6){
continue;
}
System.out.println(i);
}

				
			

Switch Statement

				
					•	The syntax of the switch statement
is as follows: Switch (expression)
{
Case Value_1:
statement(s); break;
Case value_2:
statement(s); break;
.
.
.
Case value_n:
statement(s); break;
Default:
statement(s);
}

				
			

What is exception?

  • java.lang.Exception object encapsulates the error conditions and throws it back to the running code.
  • Sub class of Throwable.
  • Class “Error” denotes the fatal errors.
  • Class “Exception” denotes non-fatal errors.
  • Helpful in providing answers to what ,how and where things went wrong in the code.

Constructs to deal with exceptions

				
					try{
//statements
} catch (Exception e)
{ //optional
//statements to handle exception
}
finally 
{ //optional
//statements to
handle exception & cleanup
}