Modifiers Java -modifiers java Exception Keywords
Modifiers keyword used
In java
modifiers are keyword which are used to change the current definition of
respected element. There are two categories of Java Modifiers
o
Access Modifier
o
Non-Access Modifier.
modifiers java Exception Keywords
Access
Modifier
Those
modifiers which showing the scope of the element are known as Access Modifier.
There are four types of Access Modifier.
o
Default: It’s scope
is within respected package except methods and data members of super class
o
Public: it’s scope
is within all over the packages, classes, interfaces etc.
o
Private: it’s scope
is within class
o Protected: It’s scope is within respected package
Non-Access
Modifier
Those
modifiers which are but definition scope but change the functionality of the
element are known as Non-Access Modifiers. Following are the type of Non-Access
Modifier
o
Static: used for create
method and variable static
o
Final: used for create
class, method and variable final
o
Abstract: used for creating
abstract class and abstract method
o
Synchronized: used for
threads
o
Volatile: used for threads.
modifiers java Exception Keywords
Access
Modifier
|
within
class
|
within
package
|
outside
package by subclass only
|
outside
package
|
Private
|
Y
|
N
|
N
|
N
|
Default
|
Y
|
Y
|
N
|
N
|
Protected
|
Y
|
Y
|
Y
|
N
|
Public
|
Y
|
Y
|
Y
|
Y
|
Exception Handling
Exception
handling is most powerful mechanism in java to control the exception which are
occurs during program execution. Basically exception is one kind of interrupt
which disturb the flow of program and exception handling remove these
disturbances and maintain the flow of the programs.
Types of exception
There are
three types of exception in which are the following Java:
o Checked Exception: those exceptions which are checked at
compile time are known as Checked Exceptions. In other words, the compiler for
fully say that to compile this piece of code you must enclosed this piece of
code within any exception handling tools. Some of possible checked exceptions
are listed below
1.
IOException
2.
SQLException
3.
DataAccessException
4.
ClassNotFoundException
5.
InvocationTargetException
6.
MalformedURLException
o Un-Checked Exception: those exceptions which are checked
at runtime are known as Un Checked Exceptions. These exceptions are caused due
to bad programming or poor programming. these exceptions are occurring when
user interacting with your application, it may be caused of wrong inputs or
invalid data etc. some of possible Un-Checked exceptions are listed below:
1.
NullPointerException
2.
ArrayIndexOutOfBoundsException
3.
ArithmeticException
4.
IllegalArgumentException
5.
NumberFormatException
o Error: when the expected outcome is different than it is
known as error. So, in programming mostly errors are uncovering because we
don’t know the occurring of errors.
Error is not an exception. When your programs are failed to give
expected result this is error.
Java Exception Handling
Keywords
In Java
there are five keywords are used for Checked and Un-Checked exception handling
o
try
o
catch
o
finally
o
throw
o
throws
modifiers java Exception Keywords
Try Catch
block
when you
write any piece of program and you think that their might be a chances of error
occurring then to prevent this type of errors you have to use try catch block.
The try block will execute your program, if any exception occur the catch block
will catch that exception and throw it at runtime.
In above
example the JVM first check any exception occur or not? If occur, then it
checks any exception handler is available or not? If no any exception handler
is available then JVM will handle its own ways, otherwise the flow of the
program will be maintained by the respected exception handler. Now as we see on
above image that exception will occur at line number five, in above example we
divide 10 by 0 so in programing division by zero is not perform that’s why it’s
trough’s the runtime exception and stop the execution of the program.
Example with try catch block
modifiers java Exception Keywords
Now in above
example we enclosed the ambiguity piece of code into try catch block, if there
is any exception then catch block will catch the exception. As we see the
exception is occurred but flow of program is not stop because of try catch
exception handler.modifiers java Exception Keywords
You can use
multiple catch block for example if you have a doubt that there may be an
Arithmetic Exception will occur or may be an Array Outs of Bounds exception
will occur then you can write multiple catch blocks surrounded by try block
try{
// code for execution
} catch (ArithmeticException
e) {
// message when exception occur
} catch (ArrayOutOfBoundsException
e) {
// message when exception occur
}
Finally,
block
Finally,
block is actually not handling the exception. This block always executes either
the exception occurs or not. This block is mainly used when you want to close
data base connection or you want to leave a message that user always get this
message. So, to handle these type of thing the finally block will execute
Example
modifiers java Exception Keywords
Now in above
example as we notice the finally block is executing either any exception occurs
or not. Now in each try block you can put more than one catch block but finally
block will be used only once.
Throw Exception
In java the
throw keyword is an explicitly throw an exception for example if we have any
piece of code and we already know that in this piece of code the chances of
exception are more then we have to use throw exception.
Example
modifiers java Exception Keywords
Now in above
program it generates a throw exception and stop the execution of the program
Throws Exception
In java the
throws keyword is used for exception handling, this throws exception handling
is used for to handle the check exception. This exception handler is helps the
programmer to handle the normal flow of the program after any exception occur. If
any unchecked exception is occurred than it’s your fault and you have to
control it and throws exception handler is not responsible for it.
Example
modifiers java Exception Keywords
No comments:
Post a Comment