answersLogoWhite

0

More answers
User Avatar

Wiki User

12y ago

Yes. Exception handling allows developers to detect errors easily without writing special code to test return values. Even better, it lets us keep exception-handling code cleanly separated from the exception-generating code. It also lets us use the same exception-handling code to deal with a range of possible exceptions.

Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. For ex: Your website displays the users bank account details. Suddenly if the bank database is down, imaging how the user would feel if an abrupt error page with numerous lines of java exception messages spewed all over the screen? First of all, it would be embarrassing and second of all, the user will get annoyed. Instead, if your code catches this database down scenario and displays a graceful message on screen saying "We are currently experiencing some difficulties in our system. Kindly try after some time. Thank you" it would be much better isn't it? That is exactly why we have or rather why we need Exception Handling.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The term "exception" means "exceptional condition" and is an occurrence that alters the normal program flow. A bunch of things can lead to exceptions, including hardware failures, resource exhaustion, and good old bugs. When an exceptional event occurs in Java, an exception is said to be "thrown." The code that's responsible for doing something about the exception is called an "exception handler," and it "catches and handles" the thrown exception.

Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. For ex: Your website displays the users bank account details. Suddenly if the bank database is down, imaging how the user would feel if an abrupt error page with numerous lines of java exception messages spewed all over the screen? First of all, it would be embarrassing and second of all, the user will get annoyed. Instead, if your code catches this database down scenario and displays a graceful message on screen saying "We are currently experiencing some difficulties in our system. Kindly try after some time. Thank you" it would be much better isnt it? That is exactly why we have or rather why we need Exception Handling.

To do this, we use the try and catch keywords. The try is used to define a block of code in which exceptions may occur. This block of code is called a guarded region (which really means "risky code goes here"). One or more catch clauses match a specific exception to a block of code that handles it. Here's how it looks in pseudocode:

1. try {

2. // This is the "guarded region"

3. // All your bank database access code goes here

4. // And any other code that might process that data

5. // We may have many code lines here

6. }

7. catch(DatabaseDownException) {

8. // Put code here that handles this exception.

9. // Put the graceful error message here

10. // This is the last line of the exception handler.

11. }

12. catch(SomeOtherException) {

13. // Put code here that handles this exception

14. }

15.

16. // Some other unguarded code continues from here

In this pseudocode example, lines 2 through 5 constitute the guarded region that is governed by the try clause. Line 7 is an exception handler for an exception of type DatabaseDownException. Line 12 is an exception handler for an exception of type SomeOtherException. Notice that the catch blocks immediately follow the try block. This is a requirement; if you have one or more catch blocks, they must immediately follow the try block. Additionally, the catch blocks must all follow each other, without any other statements or blocks in between. Also, the order in which the catch blocks appear matters. For now just know this. We will see more in detail about their order a little bit later.

Execution of the guarded region starts at line 2. If the program executes all the way past line 5 with no exceptions being thrown, execution will transfer to line 15 and continue downward. However, if at any time in lines 2 through 5 (the try block) an exception is thrown of type DatabaseDownException, execution will immediately transfer to line 7. Lines 8 through 10 will then be executed so that the entire catch block runs, and then execution will transfer to line 15 and continue.

Note that if an exception occurred on, say, line 3 of the try block, the rest of the lines in the try block (4 and 5) would never be executed. Once control jumps to the catch block, it never returns to complete the balance of the try block. This is exactly what you want, though. Imagine your code looks something like this pseudocode:

try {

getTheFileFromOverNetwork

readFromTheFileAndPopulateTable

}

catch(CantGetFileFromNetwork) {

displayNetworkErrorMessage

}

The preceding pseudocode demonstrates how you typically work with exceptions. Code that's dependent on a risky operation (as populating a table with file data is dependent on getting the file from the network) is grouped into a try block in such a way that if, say, the first operation fails, you won't continue trying to run other code that's also guaranteed to fail. In the pseudocode example, you won't be able to read from the file if you can't get the file off the network in the first place.

One of the benefits of using exception handling is that code to handle any particular exception that may occur in the governed region needs to be written only once. Returning to our earlier code example, there may be three different places in our try block that can generate a DatabaseDownException, but wherever it occurs it will be handled by the same catch block (on line 7).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How are exceptions handled in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Describe the JAVA throwable class hierarchy and types of exceptions?

In Java there are two main types of Exceptions. * Checked Exceptions - The ones that can be checked & handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch & handle these exceptions * Un-checked Exceptions - The ones that we cannot & should not handle in our code. Ex. Null Pointer Exception The java.lang.Throwable is the super class of all errors and exceptions in Java. Only objects of this class can be thrown & caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


Is exception handling implicit or explicit?

In Java, Exception Handling is Explicit. The Programmer has to write code that will ensure that the exceptions are caught and appropriately handled


What is the implication of catching all the exceptions with the type Exception?

though catching exceptions is a good practice inside java code, catching all exceptions of the type exception is not the best way to go. Specific exceptions need to be caught instead of the generic Exception being caught. Also, different types of exceptions need to be handled separately.


What are the errors in object oriented program?

In Java, errors that arise during the execution of the program are more formally referred to as Exceptions. Exceptions can be handled using try catch blocks. Here is an example : try { int answer = 42 / 0 ; } catch ( ArithmeticException e ) { e.printStackTrace(); }


What is the purpose of claiming exceptions?

it informs compiler about its possible exceptions. For example,The purpose of Java exception is to tell the Java runtime system what can go wrong

Related questions

Describe the JAVA throwable class hierarchy and types of exceptions?

In Java there are two main types of Exceptions. * Checked Exceptions - The ones that can be checked & handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch & handle these exceptions * Un-checked Exceptions - The ones that we cannot & should not handle in our code. Ex. Null Pointer Exception The java.lang.Throwable is the super class of all errors and exceptions in Java. Only objects of this class can be thrown & caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


Is exception handling implicit or explicit?

In Java, Exception Handling is Explicit. The Programmer has to write code that will ensure that the exceptions are caught and appropriately handled


What is the implication of catching all the exceptions with the type Exception?

though catching exceptions is a good practice inside java code, catching all exceptions of the type exception is not the best way to go. Specific exceptions need to be caught instead of the generic Exception being caught. Also, different types of exceptions need to be handled separately.


What are the types of exception in java?

Exceptions are of two types: checked exceptions and unchecked exceptions.


What are the errors in object oriented program?

In Java, errors that arise during the execution of the program are more formally referred to as Exceptions. Exceptions can be handled using try catch blocks. Here is an example : try { int answer = 42 / 0 ; } catch ( ArithmeticException e ) { e.printStackTrace(); }


What is the purpose of claiming exceptions?

it informs compiler about its possible exceptions. For example,The purpose of Java exception is to tell the Java runtime system what can go wrong


What is the use of try in java?

The try keyword is used in Java to handle problematic situations that are commonly known as "Exceptions" The try keyword is used in conjunction with the catch keyword. If any exception is thrown by code inside the try block, they will be caught and handled by the catch block. Ex: try { ... ... } catch (Exception e){ ... }


Difference between checked and unchecked exception?

Checked exceptions are exceptions which need to be handled explicitly. These are the ones which require a try-catchblock or a throws keyword.Unchecked exceptions are exceptions which have no obligation to be handled. A NullPointerException is one common example.


How exception subclass is created in java?

To handle the exceptions in large programs


What is exception and give a simple example in java programming?

Exception is a situation in Java wherein the system is behaving in a not-so-correct way or rather the system is behaving in an erroneous way. Exceptions are handled using try-catch blocks. Ex: try { int x = 10/0; } catch (Exception e) { e.printStackTrace(); } Here we are performing an illegal operation by dividing 10/0 which will throw a numeric exception which will be caught and handled inside the catch block


How exception handling differs in C plus plus and Java?

Exception handling is largely the same for both. The only real difference is that C++ has no 'finally' clause which always executes whether an exception occurs or not. Another difference is that Java throws exceptions and errors, but errors cannot be handled since programs cannot handle errors -- such as the JVM out of memory error.


What types of values can be thrown in java?

Only instances of subclasses of throwable can be used in conjunction with the throw keyword. In java all exceptions and errors are subclasses of throwable.