Exception Handling, Threading, and Annotations in Java
Exception Handling
Throwing an Exception
Use ‘throw’ statement to throw an exception or simply use the throw clause with an object reference to throw an exception.
The syntax is ‘throw new Exception();’. Even you can pass the error message to the Exception constructor.
Below example shows how to throw an exception.
1.package com.myjava.exceptions;
2.public class MyExplicitThrow {
3.public static void main(String a[]){
3.try{
4.MyExplicitThrow met = new MyExplicitThrow();
5.System.out.println("length of JUNK is " + met.getStringSize("JUNK"));
6.System.out.println("length of WRONG is " + met.getStringSize("WRONG"));
7.System.out.println("length of null string is " + met.getStringSize(null));
8.} catch (Exception ex){
9.System.out.println("Exception message: " + ex.getMessage());
10.}
11.public int getStringSize(String str) throws Exception{
12.if(str == null){
13.throw new Exception("String input is null");}
14.return str.length();
15.}
}Threading
Synchronization
Multi-threaded programs may often come to a situation where multiple threads try to access the same resources and finally produce erroneous and unforeseen results. So it needs to be made sure by some synchronization method that only one thread can access the resource at a given point of time.
Java provides a way of creating threads and synchronizing their task by using synchronized blocks. Synchronized blocks in Java are marked with the synchronized keyword. A synchronized block in Java is synchronized on some object. All synchronized blocks synchronized on the same object can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.
1.import java.io.*;
2.import java.util.*;
// A Class used to send a message
4.class Sender
{
6. public void send(String msg)
{
8.System.out.println("Sending\t" + msg);
9.try{
10.Thread.sleep(1000);
11.} catch (Exception e)
{
14.System.out.println("Thread interrupted.");
15.}
16.System.out.println("\n" + msg + "Sent");
17.}
}Lock
This is the base interface for Lock API. It provides all the features of synchronized keyword with additional ways to create different Conditions for locking, providing timeout for thread to wait for lock. Some of the important methods are lock() to acquire the lock, unlock() to release the lock, tryLock() to wait for lock for a certain period of time, newCondition() to create the Condition etc.
Annotations
Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.
Annotations in java are used to provide additional information, so it is an alternative option for XML and java marker interfaces.
Built-In Java Annotations types used in java code
@Override
1.class Animal{
2.void eatSomething(){System.out.println("eating something");}
}
3.class Dog extends Animal{
4.@Override
5.void eatsomething(){System.out.println("eating foods");}//should be eatSomething
6.}
7.class TestAnnotation1{
8.public static void main(String args[]){
9.Animal a=new Dog();
10.a.eatSomething();
11.}
}@SuppressWarnings
1.import java.util.*;
2.class TestAnnotation2{
3.@SuppressWarnings("unchecked")
4.public static void main(String args[]){
5.ArrayList list=new ArrayList();
6.list.add("sonoo");
7.list.add("vimal");
8.list.add("ratan");
9.for(Object obj:list)
10.System.out.println(obj);
11.}
}@Deprecated
1.class A{
2.void m(){System.out.println("hello m");}
3.@Deprecated
4.void n(){System.out.println("hello n");}
5.}
6.class TestAnnotation3{
7.public static void main(String args[]){
8.A a=new A();
9.a.n();
10.}
}Runnable Interface
1.class Multi3 implements Runnable{
2.public void run(){
3.System.out.println("thread is running...");
4.}
5.public static void main(String args[]){
6.Multi3 m1=new Multi3();
7.Thread t1 =new Thread(m1);
8.t1.start();
9.}
}