All Java exceptions and errors are subclasses of a class in the
java.lang package called Throwable. Only an object of type Throwable can be thrown in code, including exceptions and system errors.
Methods of the
Throwable class includegetMessagetoStringinitCauseprintStackTrace
getMessage- The
getMessagemethod returns an appropriateStringerror message from aThrowableobject.
ThegetMessagemethod returnsnullif the object was not created with an error message. You should provide descriptive messages for all exceptions handled in code. toString- The
toStringmethod returns a description of an exception object, which includes the exception type. initCause- The
initCausemethod sets the cause of the exception, which is always anotherThrowableobject. This method enables exception chaining. printStackTrace- You use the
printStackTracemethod to find out which method has thrown a particular exception.
Suppose you want to print out the call stack at the point that
e is caught.public class TestExceptions2 {
// Catch the exceptions within the method itself
void tryValues (int x, int y) {
boolean wasError = false ;
int[] intArray = new int[5] ;
for (int i=0; i < intArray.length - 1; i++ )
{ intArray[i] = i + 8 ; }
try {
for (int i = 0; i <= x - 1; i++) {
System.out.println (intArray[i] / y) ;
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("Array bounds exceeded" ) ;
MISSING CODE();
wasError = true ;
}
}
}
To print out the call stack, you type
e.printStackTrace.
The call stack records the series of method calls leading to the exception. When debugging, you can use
printStackTrace to help determine where the exception originated in your code.public class TestExceptions2 {
// Catch the exceptions within the method itself
void tryValues (int x, int y) {
boolean wasError = false ;
int[] intArray = new int[5] ;
for (int i=0; i < intArray.length - 1; i++ )
{ intArray[i] = i + 8 ; }
try {
for (int i = 0; i <= x - 1; i++) {
System.out.println (intArray[i] / y) ;
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("Array bounds exceeded" ) ;
e.printStackTrace();
wasError = true ;
}
}
}
The two immediate subclasses of Throwable are
ErrorException
ErrorErrorand its subclasses are used for serious system or compile-time errors that cannot or should not be handled by an application. For instance these could include the following errors -ExceptionInInitializerError,StackOverflowError, andNoClassDefFoundError.ExceptionExceptionand its subclasses are used for implementation-specific exceptions that an application might be expected to handle - for example, if a printer is switched off when the user attempts to print a document.Exceptionis the superclass of all the exceptions you can handle in your code.






4 comments:
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
Thank you for your post. I found your blog more informative and useful.
TezLyrics
Really impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing.360digitmg
شريط مانع الانزلاق
Post a Comment