Exception Handling in Java

Exception Handling in Java - Page 1 of 10 Exception...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Page 1 of 10 Exception Handling in Java An Exception is an error that occurs at runtime, as the results of an unexpected event. To prevent exceptions from crashing your program, you must write code that detects and handles exceptions. This document discusses using Exceptions for handling errors in programs, what exceptions are, how to create them, how to use them, and how to handle them when they are thrown exceptions by a program. Example 1 : Uncaught Exception . Let us look at the following program. // Demonstrate JVM handle the error in the following program. class NotHandled { public static void main(String args) { int nums = new int[4]; System.out.println("Before exception is generated."); // Generate an index out-of-bounds exception. nums[7] = 10; } } If we were to run the above program, the array index error occurs, execution is halted, and JVM will give you the following error message. Output (NotHandled Class): Before exception is generated. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at NotHandled.main(NotHandled.java:10) Press any key to continue... Exception handling fundamentals : Java exception handling is managed via five keywords: try , catch , throw , throws , and finally. Program statements that you want to monitor for exceptions are within a try block . If an exception occurs within a try block, we say the exception is thrown . Your code can catch this exception using catch clause and handle it in some rational manner. When an error occurs in a program, you can also manually throw an exception using the keyword throw . At this time an Exception object will also be created and then execution of the program will stop at that point and go back to the caller looking for a catch block. In a catch block, we can access the message that is contained in an exception that is thrown. This message is set when the exception is created and thrown and should give some meaningful information about what error has occurred. try--catch . These two keywords represent the entire mechanism for using exceptions in Java. We put code that might generate an exception into a try block. If there is a case Page 2 of 10 to throw an exception, it happens from within this block. The try is coupled with a catch that will handle the case when then exception is thrown . The following is general format of try-catch blocks try { ( try block statements …) } Catch ( ExceptionType ParameterName ) { (Catch block statements….) } First the keyword try appears. Next, a block of code appears inside braces, which are required. This block code is known as a try block . A try block is one or more statements that are executed and can potentially throw an exception....
View Full Document

This note was uploaded on 10/05/2011 for the course CS 38 taught by Professor Stevehouseman during the Summer '09 term at Irvine Valley College.

Page1 / 10

Exception Handling in Java - Page 1 of 10 Exception...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online