public static void mainString args try DatagramSocket socket new

Public static void mainstring args try datagramsocket

This preview shows page 22 - 36 out of 43 pages.

public static void main(String args[ ]) { try { DatagramSocket socket = new DatagramSocket(8282); } catch (SocketException socketEx) { socketEx.printStackTrace( ); } } } >> Lesson 3 2 3 This constructor must be call from try block because it throws an exception Catching exception object thrown by Datagram constructor To print details from thrown exception
Image of page 22

Subscribe to view the full document.

Another example : Risky Code public class DemoRiskyArray { public static void main(String[] args) { int numbers[] = {1,2,3,4,5}; try { System.out.println(numbers[5]); } catch (ArrayIndexOutOfBoundsException arrayEx) { arrayEx.printStackTrace(); } } } >> Lesson 3 2 4 java.lang.ArrayIndexOutOfBoundsException: 5 at DemoRiskyArray.main(DemoRiskyArray.java:10) Can not println numbers[5] because max is numbers[4]
Image of page 23
Did You Notice? In the previous example , catch( ) block caught two different kinds of exception objects? SocketException socketEx ArrayIndexOutOfBoundsException arrayEx How is this possible? catch( ) block is a polymorphic block. It can catch any object from any kinds of Exception class. >> Lesson 3 2 5
Image of page 24

Subscribe to view the full document.

Class Exception API >> Lesson 3 2 6 Mother of all exceptions
Image of page 25
Class ArrayIndexOutOfBoundsException >> Lesson 3 2 7 Super class Inheriting from Exception
Image of page 26

Subscribe to view the full document.

Class SocketException >> Lesson 3 2 8 Super class Inheriting from Exception
Image of page 27
Always Get Executed There will be a situation when you need to perform a cleanup whether exception occurs or not. Example: Close any file or connection. For this, we use finally block. Whether there is an exception or not, finally block will always get executed . finally block is optional . >> Lesson 3 2 9
Image of page 28

Subscribe to view the full document.

Try, catch & finally diagram >> Lesson 3 3 0
Image of page 29
Syntax of try…catch( )… finally block try { /* put risky constructors or methods here */ } catch (Exception exceptionObject) { /* Handle exceptionObject here */ } finally { /* put finally implementation here */ } >> Lesson 3 3 1
Image of page 30

Subscribe to view the full document.

Demo Of Finally public class DemoRiskyArray { public static void main(String[] args) { int numbers[] = {1,2,3,4,5}; try { System.out.println(numbers[4]); } catch (ArrayIndexOutOfBoundsException arrayEx) { arrayEx.printStackTrace(); } finally { System.out.println (“I always get executed”); } } /* What is the output? __________________________ */ } >> Lesson 3 3 2
Image of page 31
MORE CATCHES Lesson 5 >> Lesson 5 BITP 3113 Object Oriented Programming Sem I 2012/2013 Auhtored by Emma McKay-Fikri 4 1
Image of page 32

Subscribe to view the full document.

More Than One Throw A constructor or method can throw more than one exception object. Example:- public Object getData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { /* Assume there are implementations here */ } >> Lesson 5 4 2 BITP 3113 Object Oriented Programming Sem I 2012/2013 Auhtored by Emma McKay-Fikri Comma is used to separate the exceptions for method throwing more than one exception. There is a class named Object in API. Check it out!
Image of page 33
Implementing Multiple Catch /* This is a code snippet. Assume some of the object has been instantiated */ try { someObject.getData(dataFlavor); } catch (UnsupportedFlavorException unsuppEx) { unsuppEx.printStackTrace(); } catch (IOException ioEx){ unsuppEx.printStackTrace(); } >> Lesson 5 4 3
Image of page 34

Subscribe to view the full document.

Multiple Catches A try
Image of page 35
Image of page 36

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes