Download presentation
Presentation is loading. Please wait.
Published byMarcella Schooley Modified over 9 years ago
1
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling
2
SE-1020 Dr. Mark L. Hornick 2 Error handling Some methods handle error conditions by returning an error code, such as true or false String text = “hello”; boolean isEq = text.equals(“Hello”); returns a false if text is not “Hello” But sometimes it does not make sense to return an error code, since the return value means something else… int x = Integer.parseInt( “-1” ); the returned value is the equivalent integer value being read
3
SE-1020 Dr. Mark L. Hornick 3 Errors can generate Exceptions An exception represents an error condition that can occur during the normal course of program execution. int x = Integer.parseInt( “3.14”); int x = Integer.parseInt( “abc” ); When an exception occurs, or is thrown, the normal sequence of flow is terminated.
4
SE-1020 Dr. Mark L. Hornick 4 Exceptions Demo
5
SE-1020 Dr. Mark L. Hornick 5 Exceptions can be caught A thrown exception causes an exception-handling routine to be executed The exception-handling routine is said to catch the exception An exception handling routine is always provided by the Java VM – if you don’t catch an exception, the VM will …and that’s not generally desirable
6
SE-1020 Dr. Mark L. Hornick 6 You catch exceptions in your code by providing an exception- handling routine inputStr = JOptionPane.showInputDialog(null, “Input your age:”); try { age = Integer.parseInt(inputStr); JOptionPane.showMessageDialog(null, “You entered:“ +age ); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, “Input error!” ); }
7
SE-1020 Dr. Mark L. Hornick 7 Exception handling Control Flow inputStr = JOptionPane.showInputDialog(null, “Input your current age:”); try { // what happens if “old” is input? age = Integer.parseInt(inputStr); JOptionPane.showMessageDialog(null, “That’s old!”); } catch (NumberFormatException e){ JOptionPane.showMessageDialog(null, “’” + inputStr +‘ is invalid\n” +“Please enter digits only”); } // at this point, the code probably loops back to // let the user try again
8
SE-1020 Dr. Mark L. Hornick 8 Catching Exceptions Statements in the try block are executed in sequence. If one of the statements throws an exception (e.g. via the code within the Integer.parseInt() method), control is passed to the matching catch block and statements inside the catch block are executed. The execution then continues to the statement following the try- block statement, ignoring any remaining statements in the try block. If no statements in the try block throw an exception, the catch block is ignored/bypassed. Execution continues with the statement following the try-catch statement.
9
SE-1020 Dr. Mark L. Hornick 9 There are different types of Exceptions The type of exception being caught must be specified in the catch block’s parameter list. In Java, an exception is represented as an instance of the Throwable class or its subclasses. The Throwable class has two subclasses: Error Exception
10
SE-1020 Dr. Mark L. Hornick 10 A few of 60+ exception classes
11
SE-1020 Dr. Mark L. Hornick 11 Catch or let go? The Error class represents serious problems that should not be caught by ordinary applications (e.g. thread death, out-of-memory). The Exception class represents error conditions that should be caught (e.g. divide-by-0, null reference, conversion errors).
12
SE-1020 Dr. Mark L. Hornick 12 Getting information from exceptions There are two methods of the Throwable class that provide information about the thrown exception: getMessage – a String description printStackTrace – call stack dump
13
SE-1020 Dr. Mark L. Hornick 13 Catching different types of Exceptions When there are multiple catch blocks in a try-catch statement, they are checked in sequence. It is important to check more specialized exception classes before the more general exception classes. When an exception is thrown, its matching catch block is executed and the other catch blocks are ignored.
14
SE-1020 Dr. Mark L. Hornick 14 Fumbling to the VM If none of the catch blocks matches the thrown exception, the system will search down the stack trace for a method with a matching catch block. If none is found, the system (VM) will handle the thrown exception And (usually) terminate your program after calling printStackTrace()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.