This is quite easy to do, just define a sub class of Exception/Error class. Your sub-classes do not need to actually implement anything. It is their existence in the type system that allows you to use them as exceptions/errors. The Exception/Error class does not define any methods of its own.
It does, of course, inherit methods provided by Thorwable class.
Extending RunTimeException or any of its sub-classes will create a user-defined unchecked-exception and extending Exception and any other sub-class of exception will create a user-defined checked-exception.
Example: The following example demonstrates the use of a user-defined checked-exception.
class InvalidMarksException extends Exception
{ InvalidMarksException(String msg)
{ super(msg);
}
}
class MyExceptionDemo
{ static void dispGrade(int marks) throws InvalidMarksException
{ if(marks < 0 || marks > 100)
throw new InvalidMarksException
if(marks >= 75) System.out.println("S");
else if(marks >= 60) System.out.println("A");
else if(marks >= 50) System.out.println("B");
else if(marks >= 33) System.out.println("C");
else System.out.println("F");
("Marks should be in the range 0 to 100");
}
public static void main(String args[])
{ try
{ int marks = Integer.parseInt(args[0]);
dispGrade(marks);
}
catch(InvalidMarksException e)
{ System.out.println(e);
}
}
}
Example: The following example is rewritten here so as to display the marks entered as command line argument along with the error message.
class InvalidMarksException extends Exception
{ int marks;
InvalidMarksException(int marks, String msg)
{ super(msg);
}
public String toString()
{
}
}
class MyExceptionDemo1
{ static void dispGrade(int marks) throws InvalidMarksException
{ if(marks < 0 || marks > 100)
this.marks = marks;
return("InvalidMarksException["+marks+"]:"+getMessage());
throw new InvalidMarksException
if(marks >= 75) System.out.println("S");
else if(marks >= 60) System.out.println("A");
else if(marks >= 50) System.out.println("B");
else if(marks >= 33) System.out.println("C");
else System.out.println("F");
(marks, "Marks should be in the range 0 to 100");
}
public static void main(String args[])
{ try
{ int marks = Integer.parseInt(args[0]);
dispGrade(marks);
}
catch(InvalidMarksException e)
{ System.out.println(e);
}
}
}
Chained Exceptions
The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception. For example, imagine a situation in which a method throws an ArithmeticException because of an attempt to divide by zero.
However, the actual cause of the problem was that an I/O error occurred, which caused the divisor to be set improperly.
Although the method must certainly throw an ArithmeticException, since that is the error that occurred. You might also want to let the calling code know that the underlying cause was an I/O error.
To allow chained exceptions, Java 2, version 1.4 added two constructors and two methods to Throwable class. The constructors are shown here:
Throwable(Throwable causeExc)
Throwable(String msg, Throwable causeExc)
In the first form, causeExc is the exception that causes the current exception. That is, causeExc is the underlying reason that an exception occurred. The second form allows you to specify a description at the same time that you specify a cause exception. These two constructors have also been added to the Error, Exception, and RuntimeException classes.
The chained exception methods added to Throwable class are getCause() and init Cause():
Throwable getCause()
Throwable initCause(Throwable causeExc)
The getCause() method return the exception that underlies the current exception. If there is no underlying exception, null is returned. The initCause() method associates causeExc with the invoking exception and returns a reference to the exception. Thus, you can associate a cause with an exception after the exception has been created. However, the cause exception can be set only once. Thus, you can call initCause() only once for each exception object. Furthermore, if the
cause exception was set by a constructor, then you cannot set it again using initCause() method.
In general, initCause() is used to set a cause for legacy exception classes which do not support the two additional constructors described earlier. Most of Java's built-in exceptions do not define the additional constructors. Thus, you will use initCause() if you need to add an exception chain to these exceptions.
Chained exceptions are not something that every program will need. However, in chases in which knowledge of an underlying cause is useful, they offer an elegant solution.
Example:
class ChainExecDemo
{
static void demoproc()
{
NullPointerException e = new NullPointerException("top layer");
e.initCause(new ArithmeticException("Cause"));
throw e;
}
public static void main(String agrs[])
{
char a = 'a';
try
{
demoproc();
}
catch(NullPointerException e)
{
System.out.println(a);
System.out.println(e);
System.out.println("Original Cause : " +e.getCause());
}
}
}
Output:
Note: The NullPointerException constructor is not overloaded to receive cause so the only way to associate cause with it is to make use of initCause() method.
It does, of course, inherit methods provided by Thorwable class.
Extending RunTimeException or any of its sub-classes will create a user-defined unchecked-exception and extending Exception and any other sub-class of exception will create a user-defined checked-exception.
Example: The following example demonstrates the use of a user-defined checked-exception.
class InvalidMarksException extends Exception
{ InvalidMarksException(String msg)
{ super(msg);
}
}
class MyExceptionDemo
{ static void dispGrade(int marks) throws InvalidMarksException
{ if(marks < 0 || marks > 100)
throw new InvalidMarksException
if(marks >= 75) System.out.println("S");
else if(marks >= 60) System.out.println("A");
else if(marks >= 50) System.out.println("B");
else if(marks >= 33) System.out.println("C");
else System.out.println("F");
("Marks should be in the range 0 to 100");
}
public static void main(String args[])
{ try
{ int marks = Integer.parseInt(args[0]);
dispGrade(marks);
}
catch(InvalidMarksException e)
{ System.out.println(e);
}
}
}
Example: The following example is rewritten here so as to display the marks entered as command line argument along with the error message.
class InvalidMarksException extends Exception
{ int marks;
InvalidMarksException(int marks, String msg)
{ super(msg);
}
public String toString()
{
}
}
class MyExceptionDemo1
{ static void dispGrade(int marks) throws InvalidMarksException
{ if(marks < 0 || marks > 100)
this.marks = marks;
return("InvalidMarksException["+marks+"]:"+getMessage());
throw new InvalidMarksException
if(marks >= 75) System.out.println("S");
else if(marks >= 60) System.out.println("A");
else if(marks >= 50) System.out.println("B");
else if(marks >= 33) System.out.println("C");
else System.out.println("F");
(marks, "Marks should be in the range 0 to 100");
}
public static void main(String args[])
{ try
{ int marks = Integer.parseInt(args[0]);
dispGrade(marks);
}
catch(InvalidMarksException e)
{ System.out.println(e);
}
}
}
Chained Exceptions
The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception. For example, imagine a situation in which a method throws an ArithmeticException because of an attempt to divide by zero.
However, the actual cause of the problem was that an I/O error occurred, which caused the divisor to be set improperly.
Although the method must certainly throw an ArithmeticException, since that is the error that occurred. You might also want to let the calling code know that the underlying cause was an I/O error.
To allow chained exceptions, Java 2, version 1.4 added two constructors and two methods to Throwable class. The constructors are shown here:
Throwable(Throwable causeExc)
Throwable(String msg, Throwable causeExc)
In the first form, causeExc is the exception that causes the current exception. That is, causeExc is the underlying reason that an exception occurred. The second form allows you to specify a description at the same time that you specify a cause exception. These two constructors have also been added to the Error, Exception, and RuntimeException classes.
The chained exception methods added to Throwable class are getCause() and init Cause():
Throwable getCause()
Throwable initCause(Throwable causeExc)
The getCause() method return the exception that underlies the current exception. If there is no underlying exception, null is returned. The initCause() method associates causeExc with the invoking exception and returns a reference to the exception. Thus, you can associate a cause with an exception after the exception has been created. However, the cause exception can be set only once. Thus, you can call initCause() only once for each exception object. Furthermore, if the
cause exception was set by a constructor, then you cannot set it again using initCause() method.
In general, initCause() is used to set a cause for legacy exception classes which do not support the two additional constructors described earlier. Most of Java's built-in exceptions do not define the additional constructors. Thus, you will use initCause() if you need to add an exception chain to these exceptions.
Chained exceptions are not something that every program will need. However, in chases in which knowledge of an underlying cause is useful, they offer an elegant solution.
Example:
class ChainExecDemo
{
static void demoproc()
{
NullPointerException e = new NullPointerException("top layer");
e.initCause(new ArithmeticException("Cause"));
throw e;
}
public static void main(String agrs[])
{
char a = 'a';
try
{
demoproc();
}
catch(NullPointerException e)
{
System.out.println(a);
System.out.println(e);
System.out.println("Original Cause : " +e.getCause());
}
}
}
Output:
Note: The NullPointerException constructor is not overloaded to receive cause so the only way to associate cause with it is to make use of initCause() method.
No comments:
Post a Comment