/
Exceptions Exceptions

Exceptions - PDF document

emmy
emmy . @emmy
Follow
342 views
Uploaded On 2021-10-07

Exceptions - PPT Presentation

Academic ResourceCenterOverviewExceptions definition and overviewTryCatch BlocksFinally BlocksOverall ExampleChecked and Unchecked ExceptionsSummaryWhat is an exceptionIn its most general sense an ex ID: 897384

exceptions catch block blocks catch exceptions blocks block exception code time unchecked program checked java run finally compile file

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Exceptions" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1 Exceptions Academic Resource Center
Exceptions Academic Resource Center Overview • Exceptions definition and overview • Try/Catch Blocks • Finally Blocks • Overall Example • Checked and Unchecked Exceptions • Summary What is an exception? • In its most general sense,

2 an exception is a problem that appears
an exception is a problem that appears at run - time that cannot be anticipated by a general Java program and needs to be specially handled by the programmer. • Common Types of Exception: FileNotFoundException , ClassNotFoundException , IndexOutOfBoundsExc

3 eption Throwing Exceptions • When
eption Throwing Exceptions • When an exception occurs it is called being thrown. • Exceptions are thrown automatically when the program encounters them. • Aside from using a try/catch block (explained in the following slides) nothing needs to be done

4 to throw an exception. Workshop Focus
to throw an exception. Workshop Focus • This workshop will focus on IOExceptions (i.e. any exceptions having to do with reading from or writing to a file.) • Most concepts can be generalized to most exceptions. Try/Catch Blocks • The most common

5 structure in Java for handling exceptio
structure in Java for handling exceptions is a try/catch block. • Try/catch blocks allow a program to handle an exception gracefully in the way the programmer wants them to. For example, try/catch blocks will let a program print an error message (rather than

6 simply crash) if it can’t find an inp
simply crash) if it can’t find an input file. Try Blocks • Try blocks are the first part of try/catch blocks. They contain any code that might cause an exception. • For IOExceptions , put any code that is creating, reading from, writing to, or closing

7 a file inside a try block. Try Block
a file inside a try block. Try Block Side Notes • Alongside code that could trigger an exception, try blocks can (but don’t have to) also contain code that doesn’t cause exceptions. An example of this is the for loop on the previous slide. • Progra

8 ms can contain more than one try block.
ms can contain more than one try block. Additionally, it is possible to have one try block inside of another try block. Catch Blocks • The latter half of a try/catch block is the catch block. While try blocks contain code that causes an exception, catch block

9 s contain the code that handles an exce
s contain the code that handles an exception. Catch Block Arguments • Catch blocks take one argument, the type of exception they are supposed to catch (handle.) • These arguments can range from a very specific type of exception to a very general catch

10 - all category of exceptions. For examp
- all category of exceptions. For example, • Don’t forget to add import statements for your exception types as they are not part of the standard Java library. Catch Block Contents • The contents of a catch block are what you want to happen when an exc

11 eption occurs (e.g. when the file being
eption occurs (e.g. when the file being accessed doesn’t exist.) • You can either print a message or have code that does something to fix the error (e.g. ask for the file name again) Catch Block Messages • You can print any message you like inside a cat

12 ch block. • Java also has a few meth
ch block. • Java also has a few methods that are set up to help handle an exception in a specific way. A couple of these are: • getMessage (): returns an error message that’s specific to each type of exception. Pair this with a println statement. •

13 printStackTrace (): prints the type of
printStackTrace (): prints the type of error and where it occurred in the program • These methods are especially helpful for debugging and finding the reason for an exception. Catch Block Message Examples getMessage () printStackTrace () Catch Block Si

14 de Notes • Every try block needs at
de Notes • Every try block needs at least one catch block. • Try blocks can have multiple catch blocks. See the example on the right. • Catch blocks must come directly after the try block they are paired with. Finally Blocks • Finally blocks

15 are an optional addition to try/catch
are an optional addition to try/catch blocks. • Finally blocks are used for code that you want to be run no matter what happens exceptionwise with your code. They are the last thing that executes and they will execute every time you run your program.

16 • One use of a finally block would
• One use of a finally block would be to make sure a file is closed at the end of a program. Putting It All Together Checked and Unchecked Exceptions • Java has two types of exceptions, checked and unchecked. • Checked exceptions are detected dur

17 ing compile time. • Unchecked except
ing compile time. • Unchecked exceptions are not detected during compile time and will appear only during run time. Checked Exceptions • These are exceptions that are checked for by the Java compiler. • These exceptions must use try/catch blocks. If

18 the blocks are not there at compile ti
the blocks are not there at compile time, the compiler will prompt for them. • Examples include FileNotFoundException and IOException . Unchecked Exceptions • Unchecked exceptions are not looked for by the compiler. Code that could throw an unchecked

19 exception will compile successfully.
exception will compile successfully. • Unchecked exceptions do not require try/catch blocks, although they can use them. In certain cases this is a good idea. • Examples include NullPointerException and ArrayIndexOutOfBoundsException . Summary •

20 Exceptions are problems that appear at
Exceptions are problems that appear at run - time that cannot be anticipated by a general Java program and need to be specially handled by the programmer. • Exceptions are handled using try/catch blocks. • Try blocks contain all code that may cause an exce

21 ption. • Catch blocks contain code u
ption. • Catch blocks contain code used to handle exceptions. • Finally blocks are optional additions to try/catch blocks that will run at the end of a program every time it runs Summary, cont. • Java has two types of exceptions, checked and unchecked.

22 • Checked exceptions are detected d
• Checked exceptions are detected during compile time. These exceptions must use a try/catch block. • Unchecked exceptions are not detected during compile time and will appear only during run time. These exceptions may or may not use a try/catch block.