answersLogoWhite

0

Exception handling in Java is done through a try-catch-finally block. The "try" block of code is where you put the code which may throw an exception. The "catch" block of code is where you put the code which will execute after an exception is thrown in the try block. This is often used to display an error message, or to mitigate problems caused by the exception. The "finally" block is where you put code that you want to execute after the try and catch blocks have been processed.

// example code for catching exception while reading from a file

try {

// this line of code can throw a FileNotFoundException

FileReader in = new FileReader("myfile.txt");

// this line of code can throw an IOException

in.read();

} catch(FileNotFoundException ex) { // catch first exception type

System.err.println("Cannot find myfile.txt!");

} catch(IOException ex) { //catch second exception type

System.err.println("Cannot read from myfile.txt!");

} finally { // no matter what we want to close the file, so do it here

// however, this line can also cause an exception, so we need to catch that, too

try {

in.close();

catch(IOException ex) {

// not much we can do about an exception that occurs here

}

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

A program to explain the Exception Handling mechanisms in Java using the keywords try catch and finally?

Here is a code snippet illustrating exception handling: try { int a= 3 / 0 ; } catch ( ArithmeticException e ) { System.out.println ("An ArithmeticException has occured"); } finally { // some code }


What is difference between exception handling in C and Java?

Easy: there is no exception-handling in C.


Can exception handling resolve the abnormal flow of a program?

An exception is abnormal program flow, so handling the exception is the very definition of resolving the abnormality.


What is the exact difference between interrupt handling and exception handling?

interrupt handling is the process of handling a break or interrupt called by a program where as exception handling is for handling some exceptional conditions that'll occur when a program is running


When is exception handling necessary for stream handling?

Exception handling is necessary for string handling as there might be some unexpected situation during string handling which may lead to program crash or abrupt termination


Why you uses exception handling because you can handle exception using if Else statement?

We use exception handling so that the program can gracefully handle any situation that may be unexpected. We use try-catch for exception handling. if-else is a conditional logic checking mechanism


When should we use exception handling in java?

Exception handling should be used in Java in all cases where you as a programmer suspect that your code might throw some exceptions or create errors that might look ugly when a user is using the application. In such cases you use exception handling to catch and handle the exception and exit gracefully. You use the try - catch block in Java for exception handling.


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 exception handling in C plus plus?

Exception handling is the means by which exceptions -- anomalous events -- are resolved without causing a runtime error.


How do you handle multiple exception?

With multiple catch clauses, one for each exception that may be thrown.


Which is the base class of exception handling in java?

Thorwable


Why do you need exception handling codes in a program?

separating error handling code from 'regular' code