You are on page 1of 10

Labeled Statements:-

 A label statement must be placed just before the statement


being labeled, and it consists of a valid identifier that ends with a colon (:).
public class LabelDemo{
public static void main(String[] args){
boolean isTrue = true;
go2:
for(int i=0; i<5; i++) {
while (isTrue) {
System.out.println("Hello");
break go2;
} // end of inner while loop
System.out.println("Outer loop."); // Won't print
} // end of outer for loop
System.out.println("Good-Bye");
}
}
 Labeled continue and break statements must be inside the loop that
has the same label name; otherwise, the code will not compile.
Handling Exceptions:-
An exception is a problem that arises during the execution of a program. An exception can occur for
many different reasons, including the following:
 A user has entered invalid data.
 A file that needs to be opened cannot be found.
 A network connection has been lost in the middle of communications or the JVM has  out of
memory.
Exception Hierarchy
Checked exceptions: A checked exception is an exception that occurs at compile time.
Runtime exceptions: A runtime exception is an exception that occurs at run time.
Errors: These are not exceptions at all and cannot be handle programmatically like out of memory.
Java provides specific key words for exception handling :-
throw:- Sometime we might want to generate exception explicitly in our code, like to avoid duplicate user in user
table. throw keyword is used to throw exception to the runtime to handle it.
throws:- When we are throwing any exception in a method and not handling it, then we need to
use throws keyword in method signature to let caller program know the exceptions that might be thrown by the
method. The caller method might handle these exceptions or propagate it to it’s caller method using throws
keyword. We can provide multiple exceptions in the throws clause and it can be used with main()method also.
try-catch:- We use try-catch block for exception handling in our code. try is the start of the block and catch is at
the end of try block to handle the exceptions. We can have multiple catch blocks with a try and try-catch block
can be nested also. catch block requires a parameter that should be of type Exception.
finally:- finally block is optional and can be used only with try-catch block. Since exception halts the process of
execution, we might have some resources open that will not get closed, so we can use finally block. finally block
gets executed always, whether exception occurred or not.

Uncaught Exceptions:- Exceptions that are not handling by the programs called uncaught exceptions.
first we will see stack or call stack. The call stack is the chain of methods that your program executes to get to
the current method. If your program starts in method main() and main() calls method a(), which calls method
b(), which in turn calls method c(), the call stack consists of the following:
c
b
a
main
package test;
public class UncaughtExDemo{
public static void main(String[] args) {
a();
System.out.println("Rest of code");
}
static void a() {
b();
}
static void b() {
int x = 5 / 0; // Can't divide by zero!
// ArithmeticException is thrown here
}
}
Output:-
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.UncaughtExDemo.b(UncaughtExDemo.java:13)
at test.UncaughtExDemo.a(UncaughtExDemo.java:9)
at test.UncaughtExDemo.main(UncaughtExDemo.java:5)

• An exception that's never caught will cause your application to stop running.
• An exception in the JVM halting and the stack trace will be printed to the output.
Handling Exception:-
package test;
public class CaughtExDemo{
public static void main(String[] args) {
a();
System.out.println("Rest of code");
}
static void a() {
b();
}
static void b() {
try{
int x = 5 / 0; // Can't divide by zero!
// ArithmeticException is thrown here
}
}
catch(ArithmeticException ex){
ex.printStackTrace();
System.out.println(ex.getMessage());
}
}
The throws Keyword:-
If a method does not handle a checked exception, the method must declare it using the throws
keyword.
* It must be use after method declaration ,out side of a method.

package test;
public class ThrowsExDemo{
public static void main(String[] args) {
a();
System.out.println("Rest of code");
}
static void a() {
b();
}
static void b() throws Exception{ // method a() that is caller for b must be handle with
try //catch or throws it.
int x = 5 / 0; // Can't divide by zero!
// ArithmeticException is thrown here
}
}
The throw Keyword:-
Syntax:- throw ThrowbleInstance;
• It must be use inside method body.
public class ThrowDemo{
public static void main(String[] args) {
a();
System.out.println("Rest of code");
}
static void a() {
b();
}
static void b() {
int a=1;
if(a==1){
throw new RuntimeException("The value of a can't be 1");
}}}
Note:- If we want to use the message of RuntimeException in caller function see next example.
public class Student {
public static void main(String[] args) {
a();
System.out.println("Rest of code");
}
static void a() {
try {
b();
} catch (RuntimeException ex) {
System.out.println(ex.getMessage());
}}
static void b() {
int a=1;
if(a==1){
throw new RuntimeException("The value of a can't be 1");
}}}

The finally Keyword:-


The finally block always executes when the try block exits. finally block is optional and Since exception halts the
process of execution, we might have some resources open that will not get closed, so we can use finally block.
public class FinalyDemo{
public static void main(String[] args) {
try{ int i = 10/0;
} catch(Exception ex){
System.out.println("Inside catch Block when exception occurs.");
} finally {
System.out.println("Inside finally block.It always execute."); }}}
public class ErrorDemo{
public static void main(String[] args) {
try{
long data[] = new long[100000000];
} catch(Exception ex){
System.out.println("Inside catch Block when exception occurs.");
} finally {
System.out.println("Inside finally block.It always execute.");}}}
Output:-
Inside finally block.It always execute.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at ErrorDemo.main(ErrorDemo.java:10)

You might also like