Sunday, July 7, 2013

Exception


Exception  Handling


Exception :
It is a technique of processing problems that occur during the execution of program.


Difference between Exception and Error :
Exception: It is unwanted and unexpected event that disturb the normal flow of program execution.

Error : Something that goes wrong in our application or program that leads to crash your output or logic.


How Exception  Handling Works ?

When  a problem occurs exception object is created, thrown and then handled.




Exception Handling Keyword:

Try- catch
Throw
Throws
Finally



Java Exception Hierarchy:




Types  of Exceptions:

Checked Exception : These are the exceptions that the java compiler enforces you to provide either by using try-catch mechanism or throws declaration mechanism. If this requirement is not met ,the compiler will issue an error messages indicating that the exception must be caught or declared.

Various checked exceptions defined in the java.lang.package are,
1.    ClassNotFoundException
2.    IllegalAccessException
3.    InstantiationException
4.    NoSuchMethodException

Unchecked Exception: These are the exceptions that the java compiler doesnot enforces you to explicity provide in the program code.


Various Unchecked Exceptions are,
1.    ArithmeticException
2.    ArrayIndexOutOfBoundsException
3.    ArrayStoreException
4.    ClassCastException
5.    IllegalArgumentException
6.    NegativeArraySizeException
7.    NullPointerException
8.    NumberFormatException


The java.lang package defines several classes and exceptions.


Exceptions
Description
Checked
Unchecked
ArithmeticException
Arithmetic  Errors such as divide by zero.
-
Yes
ArrayIndexOutOfBoundsException
Array index is not within array.length
-
Yes
ClassNotFoundException
Related Class not found
Yes
-
IOException
Input/Output field not found
Yes
-
IllegalArgumentException
Illegal arguments when calling a method
-
Yes
InterruptedException
One thred has been interrupted by another thread
Yes
-
NoSuchMethodException
Noexistent  method
Yes
-
NullPointerException
Invalid use of null reference
-
Yes
NumberFormatException
Invalid string for conversion to number
-
Yes



Exception Control Flow:





Getting  Information From Exception:


List of Method defined by Throwable  Class.


Methods
Description
String toString( )
Gives you a String object and description of the exception. This  methods is called by the println ( ) method when an object of throwable is passed to it as argument.
String getMessage( )
Gives you the description of the exception in program.
Throwable fillInStackTrace( )
Gives you aThrowable Object that contains a stack trace.
void print StackTrace( )
Gives you and print the stack trace
void printStackTrace(PrintStream stream)
Return the stack trace to a specific defined stream
String getLocalizedMessage
Return the Localized description of the exception














Throw: When you want to throw an exception explicitly , then use throw keyword.

 





Throws: This is  used when you are  not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions). 









Output:
  • A new file named test will be created.




Finally: If an exception occur inside a try-catch block and there is no matching catch block, the method terminates without further executing lines of code .
If  you want that lines of code must be executed regardless of whether or not there is matching cathch block, put those lines inside the catch block.





Defining Your Own Exceptions:
You  can create your  own customized exception as per requirements of the application. On each application there is a specific constraints. For this, you create your own customized Exception defining all the constraints and ensure the integrity in the application.


















Wednesday, March 6, 2013

Java Basic

Java Basics 



Before we begin our discussion about basic stuff of  "Java", Let us start from the scratch, which we all know but never give so much concern.

FAQ:1 Why just 0s and 1s?

Computers use 0s and 1s because it is easy to make an electrical device that has only two stable states. However, when you are programming, you normally need not be concerned about the encoding of data as 0s
and 1s. You can program as if the computer directly stored numbers,letters, or strings of characters in memory.
There is nothing special about calling the states zero and one. We could just as well use any two names, such as A and B or true and false.

The important thing is that the underlying physical device has two stable states, such as on and off or high voltage and low voltage. Calling these two states zero and one is simply a convention, but it’s one that is almost
universally followed.

FAQ:2 A SIP OF JAVA !~!


Java n. An island of Indonesia, 48,842 square miles in area, lying between
the Indian Ocean and the Java Sea.

Java n. Informal. Brewed coffee.


Now Comes the Topic of Discussion :-

Bytecode

Before we explain Bytecode, let us take an example form Real World !~!

Stenography : The process of writing in Shorthand.

Bytecode - is a Shorthand, which converts Source code into specific Bytes of Code, which can be understand by Sun MicroSystems, now known by Oracle.















How it is Inter-operable with Different Operating System ?

It require a virtual environment which will be compatible with that ByteCode, i.e. JVM


JVM 

JVM (Java Virtual Machine) is a software which creates its own environment, that will provide a virtual environment to the Bytecode, which takes Bytecode into Memory & along with Operating System, it convert in machine code.It will also call O.S function calls.

OS vendor, just download JVM API, and it will integrate its own 20% file & make its own JVM compatible to different O.S.It makes JRE for each Operating System


The Java Virtual Machine (JVM) gives Java its platform independence. One of the primary design goals of the Java language is to enable the same code to run on any platform. The JVM is a software program that behaves like an entire computer. By using this artificial computer on different computer platforms (UNIX, Win32, Macintosh, and so forth),

You can reuse programs without creating a version for each platform. Your Java programs will always run on a JVM. A Java program can be run on any platform for which a JVM is available.



Class
class -A type of container into which all Java code must be placed. Also, the template or blueprint for an object.

class HelloWorld
{ -------- Statement  -------------}


When you write a stand-alone application, the Java Virtual Machine must know where to begin executing the code. To begin executing code, the JVM looks for and calls a special method by the name of,
public static void main(String[] args). 

The JVM uses this method to begin executing your program. 
Study the following code example:

class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello Code-Crazy Guys !~!~! ");
}
}



JRE

The Java Runtime Environment (JRE), which provides the JVM. The development tools and compilation libraries necessary to create Java applications, servlets and applets. This grouping includes the Java compiler, debugging programs, and tools to run applets without a browser.