Java 6
Java 6
Java 6
An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:
A user has entered invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications, or the JVM has run out of memory. Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. To understand how exception handling works in Java, you need to understand the three categories of exceptions: Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation. Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compliation. Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Program statements that you want to check for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some normal manner. System-generated exceptions are automatically thrown by the Java runtime system.To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. 350703 JAVA PROGRAMMING Page 1 Rahul Patel
Using try and catch The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code. With the help of try and catch we solve the run time error easy. The general syntax of the try/catch statement is as below: try { body-code } catch (exception-classname variable-name) { handler-code } The try/catch statement has four parts. The body-code contains code that might throw the exception that we want to handle. The exception-classname is the class name of the exception we want to handle. The variable-name specifies a name for a variable that will hold the exception object if the exception occurs. Finally, the handler-code contains the code to execute if the exception occurs. After the handler-code executes, execution of the thread continues after the try/catch statement. Advantage of using try & catch: It allows the programmer to fix the error. It prevents the programmer from automatically terminating. Example:class Trycatch { public static void main (String[] args) { try { int a,b,c; a=12; b=0; c=a/b; System.out.println(c); } catch(Exception e) { System.out.println(e); } } 350703 JAVA PROGRAMMING Page 3 Rahul Patel
Page 6
Rahul Patel
Page 9
Rahul Patel
Page 12
Rahul Patel