{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exception Handling in Java

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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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. catch Block Parameter . After the try block, a catch clause appears. A catch clause begins with the keyword catch . When a catch block is created, it must specify what type of exception it will catch and gives that exception a name (identifier) and a (Parameter name) that will be used inside of the catch block when referring to the exception that was caught.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern