Hi guys today we are going to see the advanced topic on JAVA , we can see what makes exception in java,
An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error. Exception handling is a mechanism to handle runtime errors. The main advantage of exception is that a normal flow of the application is maintained.
Exception Handling Fundamentals
Java Exception handling is handled by five keywords They are,
* try
* catch
* throw
* throws
* finally
Program statements that you want to monitor for exceptions are contained within a try block. If an exceptions are contained within a try block , it is thrown. Your code can catch this exception using catch and handle it in rational manner. System-generated exception are automatically thrown by the java-time system.To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clauses. Any code that absolutely must be executed after a try block completes is put in a finally block. This is the general form of an exception-handling block.
try
{
//block of code to monitor for errors
}
catch(ExceptionType1 exob)
{
//exception handler for exception type1
}
catch(Exception Type2 exob)
{
// exception handler for exceptionType2
}
//....
finally
{
//block of code to be executed after try block ends
}
Here,ExceptionType is the type of exception that has occurred
* Checked Exception
* Unchecked Exception
* Error
An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error. Exception handling is a mechanism to handle runtime errors. The main advantage of exception is that a normal flow of the application is maintained.
Exception Handling Fundamentals
Java Exception handling is handled by five keywords They are,
* try
* catch
* throw
* throws
* finally
Program statements that you want to monitor for exceptions are contained within a try block. If an exceptions are contained within a try block , it is thrown. Your code can catch this exception using catch and handle it in rational manner. System-generated exception are automatically thrown by the java-time system.To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clauses. Any code that absolutely must be executed after a try block completes is put in a finally block. This is the general form of an exception-handling block.
try
{
//block of code to monitor for errors
}
catch(ExceptionType1 exob)
{
//exception handler for exception type1
}
catch(Exception Type2 exob)
{
// exception handler for exceptionType2
}
//....
finally
{
//block of code to be executed after try block ends
}
Here,ExceptionType is the type of exception that has occurred
Type of Exception:
There are mainly two types of exception: checked and unchecked where error is considered as unchecked exception. The sun microsystem says there are three types of exception* Checked Exception
* Unchecked Exception
* Error
CHECKED EXCEPTION:
This classes that extend throwable class except RunTimException and error are known as checked exceptions (e.g) IOException, SQLExcepion etc. Checked exceptions are checked at compile-time.
UNCHECKED EXCEPTION:
This classes that extend RunTimeException are known as unchecked exception e.g ArithmeticException, NullPointerException, ArrayIndexOutofBoundsException etc. Unchecked exception are not checked at compiled time rather they are checked at runtime
ERROR:
Error is irrecoverable e.g OutOfMemoryError, VirtualMachineError, AsserionError etc. using try and catch block to handle runtime error, enclose the code that you want to monitor inside a try block . Immediately after the try block, include a catch clause that specifies the exception type that you wish to catch. The followinf program includes try block and a catch clause that processes the arithmeticException henerated by the division by zero error. Once an exception is thrownm program control transfers out of the try block into the catch block.
Example:
class Exc1
{
public static void main(String args[])
{
system.out.println(using try and catch block);
int d,a;
try //monitor a block of code.
{
d=0;
a=42/d;
system.out.println(This will not be printed);
}
catch(ArithmeticException e)
//catch divide-by-zero
error
{
system.out.println(division by zero);
}
system.out.println(after catch statements);
}
}
MULTIPLE CATCH BLOCK:
In some cases, more than one exception could be raised by a single piece of code. To handle this type of situation, we can use two or more catch clauses, each catching a different type of exception. When an exception is thrown , each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes the others are bypassed, and execution continues after the try/catch block.
THROW KEYWORD:
The keyword is used to explictily throw an exception.We can throw either checked or unchecked exception.The general form is.
throw Throwableinstance;
Throwableinstance must be an object of type Throwable or a subclass of throwable. There are two ways you can obtain a throwable object: using a parameter in a catch clause, or creating one with the new operator. The flow of execption stops immeditely after the throw statements; any subsequent statements are not executed.
Example:
class throwdemo
{
staticvoid demproc()
{
try
{
throw new NullpointerExcepion(demo);
}
catch(NullpinterException e)
{
system.out.println(caught inside demoproc.);
throw e; //rethrow the exception
}
}
public static void main (Static args[])
{
system.out.println(Throw example);
try
{
demoproc();
}
catch(Nullpointereception e)
{
system.out.println(Recaught +e);
}
}
}
New is used to construct an instance of NullPointerException . Many of java's built in run times exceptions
That's all for today guys this is exception and this is in java... see you later in next class guys
No comments:
Post a Comment