Prezi

Share this prezi

Who can edit:

Present Online

Send the link below via email or IM to invite your audience

Copy

Start the presentation

Start presenting

  • Invited audience will follow you as you navigate and present
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can view together your prezi
  • Learn more about this feature in the manual

Download prezi for:

Present offline on a PC or Mac.

  • Embedded YouTube videos need an active Internet connection to play.
  • Portable prezis are not editable.

Edit and present offline with Prezi Desktop

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

Exceptions

No description
by Don Bogardus on 18 September 2012

Comments (0)

Please log in to add your comment.

Report abuse

Prezi Transcript

Exceptions Events which disrupt the normal flow of a program MINDSHARE Java 101 Print "Foo" Throw an Exception throw new RuntimeException(); Create a new class named Foo which prints "Foo" to the console Throw exception from a method Create a new method called 'bar', throw the exception from there instead Subclass Exception Create class "IcebergException" which extends RuntimeException, throw that instead What Happens? Object o = null; o.toString(); public class Foo { public static void main(String[] args){ System.out.println("Foo"); throw new RuntimeException(); } } public class Foo { public static void main(String[] args){ System.out.println("Foo"); } } public class Foo { public static void main(String[] args){ System.out.println("Foo"); bar(); } public static void bar(){ throw new RuntimeException(); } } public class Foo { public static void main(String[] args){ System.out.println("Foo"); throw new IcebergException(); } } class IcebergException extends RuntimeException {} What Happens? Object o = new Object(); Foo foo = (Foo)o; Checked vs. Unchecked Exceptions "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception." Exercise Change RuntimeException to Exception in previous example Options for Handling Checked Exceptions Don't Log and Rethrow - Why? Find some way around the problem Give up and notify someone of the problem (Email, Log, Message to the user) 'Rethrow' the Exception Source - http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.htm What Happens? String[] stringArray = {"0","1","2"}; System.out.println(stringArray[3]); What Happens? Integer i = new Integer("Not a number"); What Happens? public static void main(String[] args){ bar(); } public static void bar() { bar(); } public class Foo { public static void main(String[] args){ doSomething(null); } public static void doSomething(Object o) { if( o == null ){ throw new IllegalArgumentException("object cannot be null"); } } } IllegalArgumentException You'll throw this when an operation is called during an inappropriate time. Example - Running a report during a maintenance window IllegalStateException Just like it sounds. How could you cause this? OutOfMemoryError If you see and Exception in our app Don't say "It Blew Up" Give the name of the exception like "NullPointerException" If there are any "caused by" notations in the stacktrace, the bottom one is usually the root cause Google the package names to get a clue Real World Examples
See the full transcript