Lets explain that what is static in java.
if we make a class static then it means members of this class will be accessed directily by class name.and we can not create instanse of this class.
only static mamber can access to other static member.
if any non-static member want to access any non-static member then it need to access throught the instance of that class
Aptitude
(12)
C
(2)
C++
(10)
CSS
(1)
Data Structure
(2)
English
(6)
Fitness
(2)
General
(14)
HTML
(3)
Integration
(1)
Itinerary and Travel Cost
(2)
Java
(29)
JQ
(5)
JS
(5)
JSP
(1)
Lightning
(14)
logical
(3)
Lyrics
(1)
Math
(1)
Music Notation
(20)
MySql
(4)
Network
(1)
Regex
(1)
salesforce
(131)
Servlet
(4)
SF Admin
(31)
SF Dev
(102)
Spiritual Content
(3)
SQL
(4)
SQL_SERVER
(1)
StudyPoints
(2)
Trips
(1)
ytChannels
(2)
अन्ताक्षरी
(4)
Custom Exception
Defining your own Exception class in JAVA
Now you know how to write exception handlers for those exception objects that are thrown by the runtime system, and thrown by a method in the standard class library.
It is also possible for you to define your own exception classes and to cause objects of those classes to be thrown whenever an exception occur. In this case, you get to decide just what constitutes an exceptional condition. For example, suppose you could write a data-processing application that processes integer data obtained via a TCP/IP link from another computer. If the specification for the program indicates that the integer value 100 should never be received, then you could use an occurrence of the integer value 100 to cause an exception object of your own design to be thrown.
Exception Types
Mainly Exceptions are of two types; checked and unchecked exceptions:
The two existing subclasses of Throwable are Exception and Error. Example-1
This example is making your own Exception class by using constructor.
// this is a user define exception class etend by Exception class
class Myexception extends Exception
{
public Myexception(int i)
{
System.out.println("you " +i +" entered It exceeding the limit");
}
}
public class ExceptionTest
{
public void show(int i) throws Myexception
{
if(i>100)
throw new Myexception(i);
else
System.out.println(+i+" is less then 100 it is ok");
}
public static void main(String []args)
{
int i=Integer.parseInt(args[0]);
int j=Integer.parseInt(args[1]);
ExceptionTest t=new ExceptionTest();
try{
t.show(i);
t.show(j);
}
catch(Throwable e)
{
System.out.println("catched exception is"+e);
}
}
}
OUTPUT
Example-2
This example is making your own Exception class by using super keyword.
class MyException extends Exception
{
MyException(String s)
{
super(s);
}
public String toString()
{
return(" " + getMessage());
}
}class ThrowClass {
int age;
ThrowClass(int age) throws MyException
{
this.age=age;
}
void getAge(int age) throws MyException
{
if(age <18 )
{
throw new MyException("Invalid Age");
}
else
{
this.age=age;
}
}
}
class TestException {
Now you know how to write exception handlers for those exception objects that are thrown by the runtime system, and thrown by a method in the standard class library.
It is also possible for you to define your own exception classes and to cause objects of those classes to be thrown whenever an exception occur. In this case, you get to decide just what constitutes an exceptional condition. For example, suppose you could write a data-processing application that processes integer data obtained via a TCP/IP link from another computer. If the specification for the program indicates that the integer value 100 should never be received, then you could use an occurrence of the integer value 100 to cause an exception object of your own design to be thrown.
Exception Types
Mainly Exceptions are of two types; checked and unchecked exceptions:
- Error and Runtime Exception are unchecked these exception not handled or check by compiler that you handle them explicitly
- All other Exception are checked that is compiler enforce or check that you can handle them explicitly
- Running out of memory
- Infinite recursion
- Inability to link
- Use the Exception class in the API.
- Create a Custom Exception class if the predefined class is not sufficient.
- Declare a custom exception classes by extending the Exception class or a subclass of Exception.
The two existing subclasses of Throwable are Exception and Error. Example-1
This example is making your own Exception class by using constructor.
// this is a user define exception class etend by Exception class
class Myexception extends Exception
{
public Myexception(int i)
{
System.out.println("you " +i +" entered It exceeding the limit");
}
}
public class ExceptionTest
{
public void show(int i) throws Myexception
{
if(i>100)
throw new Myexception(i);
else
System.out.println(+i+" is less then 100 it is ok");
}
public static void main(String []args)
{
int i=Integer.parseInt(args[0]);
int j=Integer.parseInt(args[1]);
ExceptionTest t=new ExceptionTest();
try{
t.show(i);
t.show(j);
}
catch(Throwable e)
{
System.out.println("catched exception is"+e);
}
}
}
OUTPUT
Example-2
This example is making your own Exception class by using super keyword.
class MyException extends Exception
{
MyException(String s)
{
super(s);
}
public String toString()
{
return(" " + getMessage());
}
}class ThrowClass {
int age;
ThrowClass(int age) throws MyException
{
this.age=age;
}
void getAge(int age) throws MyException
{
if(age <18 )
{
throw new MyException("Invalid Age");
}
else
{
this.age=age;
}
}
}
class TestException {
public static void main(String [] args)
{
int a=Integer.parseInt(args[0]);
try {
ThrowClass t=new ThrowClass(a);
t.getAge(a);
System.out.println(t.age);
}
catch(MyException me)
{
System.out.println(me);
}
}
}
{
int a=Integer.parseInt(args[0]);
try {
ThrowClass t=new ThrowClass(a);
t.getAge(a);
System.out.println(t.age);
}
catch(MyException me)
{
System.out.println(me);
}
}
}
OUTPUT
Subscribe to:
Posts (Atom)