Uncaught Exceptions Assignment Help

Assignment Help: >> Exception Types - Uncaught Exceptions

Uncaught Exceptions:

Before you learn how to handle exceptions within your program, it is useful to see what happens whenever you don't handle them.  That small program involves an expression which intentionally causes a divide through zero error.

Class Exc0 {

public static void main(string   args[]) {

int d= 0;

int a =42 / d;

}

}

Whenever the java run time system detects the attempt to divide by zero, it constructs a new exception object and after that throws this exception.  This causes the execution of EXC0 to stop, since once an exception has been thrown, it must be caught through an exception handler and dealt along with instantly.  In this instance, we haven't supplied any exception handlers of our own, and then the exception is caught via the default handler gives through the Java run-time system.  Any exception which is not caught through your program will ultimately be processed by the default handler.  A default handler shows a string describing the exception, prints a stack trace from the point at that the exception occurred or terminated the program.

Here is the output produced when this example is executed through the standard Java JDK run-time interpreter:

java.lang.ArithmeticException:/ by zero at

Exc0.main(Exc0.java:4)

Note that how the class names, Exc0; the method name as main; the filename, Exc0.java; or the line number 4, is all involved in the easy stack trace.

Also, note that the type of the exception thrown is a subclass of Exception is known as Arithmetic Exception that more specifically describes what type of error happened.  As disculled later in this lesson Jave suppplies various built in exception types which match the several sorts of run-time errors which can be produced.

The stack trace will always display the sequence of method invocations which led up to the error.  For instance, here is another version of the preceding program which introduces the similar error but in a method separate from main ();

class Excl{

static void subroutine() {

int = 0;

int a =10/d;

}

public static void main (string args[]) {

Excl.subroutine();

}

}

The concluding stack trace from the default exception handler displays how the whole call stack is shown;

java.lang.ArithmeticException : / by zero

at Excl.subrooutine(Excl.java:4)

at Excl.main(Excl.java:7)

As you could see, the bottom of the stack is main's line 7 that is the call to subroutine () that caused the exception at line4. A call stack is quite useful for debugging, since it pinpoints the precise sequence of steps which led to the error.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd