Exception Handling Exception Handling Keywords User Defined Exceptions Assertions
Exception Handling Exception Handling Keywords User Defined Exceptions Assertions
Exception Handling Exception Handling Keywords User Defined Exceptions Assertions
Exception Handling
Three categories of errors: syntax errors, runtime errors, and logic errors Syntax errors arise because the rules of the language have
not been followed. They are detected by the compiler. Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out. Logic errors occur when a program doesn't perform the way it was intended to. In general, syntax errors are easy to find and easy to correct because the compiler indicates where they came from and why they occurred. Exception Handling introduces to deal with runtime errors.
Exception Handling(continued)
An exception is a runtime error. A program that does not provide code for catching and
handling exceptions will terminate abnormally, and may cause serious problems. Exceptions occur for various reasons:
invalid input, an array outside the bounds of the array etc.
the program will be interrupted. Java provides programmers with the capability to handle runtime exceptions. With this capability, referred to as exception handling, we can develop robust programs for mission-critical computing.
Exception Handling(continued)
Here is an example. The program terminates abnormally if we enter a floating-
import java.util.Scanner; public class EceptionDemo{ public static void main(String [] args){ Scanner s = new Scanner(System.in); System.out.print("Enter an integer:"); int n = s.nextInt();
Exception Handling(continued)
import java.util.*; public class HandleEceptionDemo{ public static void main(String [] args){ Scanner s = new Scanner(System.in); boolean cInput = true; do { try{ System.out.print("Enter an integer:"); int n = s.nextInt(); System.out.println("The number entered is " +n); cInput = false; } catch(InputMismatchException ex){ System.out.print("Try again.(" + "Incorrect Input:an integer is required)"); s.nextLine(); } }while(cInput); } }
caused by your program and by external circumstances. These errors can be caught and handled by your program. Examples of subclasses of Exception:
Class ClassNotFoundException
Possible Reason for Exception Attempt to use a class that does not exist. This exception would occur, for example, if you tried to run a nonexistent class using the java command, or if your program was composed of, say, three class files, only two of which could be found. CloneNotSupported Exception Attempt to clone an object whose defining class does not implement the Cloneable interface. IOException Related to input/output operations, such as invalid input, reading past the end of a file, and opening a nonexistent file. Examples of subclasses of IOException are InterruptedIOException, EOFException (EOF is short for End Of File), and FileNotFound Exception. AWTException Exceptions in GUI components.
which describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the JVM. Examples of subclasses are:
Class
ArithmeticException
Possible Reason for Exception Dividing an integer by zero. Note that floating-point arithmetic does not throw exceptions. Attempt to access an object through a null reference variable. Index to an array is out of range. A method is passed an argument that is illegal or inappropriate.
as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with them. In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. For example,
through a reference variable before an object is assigned to it; an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are logic errors that should be corrected in the program.
operations:
an IOException. If the method might throw multiple exceptions, add a list of the exceptions, separated by commas, after throws:
public void myMethod()
an appropriate exception type and throw it. This is known as throwing an exception. Here is an example:
Suppose the program detected that an argument passed to
the method violates the method contract (e.g., the argument must be non-negative, but a negative argument is passed); the program can create an instance of IllegalArgumentException and throw it, as follows:
IllegalArgumentException ex = new IllegalArgumentException("Wrong Argument"); throw ex;
Tip
The keyword to declare an exception is throws, and
whether an exception occurs or is caught. Java has a finally clause that can be used to accomplish this objective. The code in the finally block is executed under all circumstances, regardless of whether an exception occurs in the try block or is caught. The syntax for the finally clause might look like this:
try { statements; } catch (TheException ex) { handling ex; } finally { finalStatements; }
own exception classes. However, if we run into a problem that cannot be adequately described by the predefined exception classes, we can create our own exception class, derived from Exception or from a subclass of Exception, such as IOException.
Example:
class MyException extends Exception { private int detail; MyException(int a) { detail = a; } public String toString() { return "MyException[" + detail + "]"; } } class ExceptionDemo { static void compute(int a) throws MyException { System.out.println("Called compute(" + a + ")"); if(a > 10) throw new MyException(a); System.out.println("Normal exit"); } public static void main(String args[]) { try { compute(1); compute(20); } catch (MyException e) { System.out.println("Caught " + e); } } }
Thank You