Results 1 to 6 of 6
  1. #1
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default Help with exception catching

    Hi i'm having trouble doing the try catch in my methods.
    Could someone give me some pointers to where I can put them?

    Java Code:
     public static void openFlight()
       {
          Flight Flight;
          String FlightID;
    
          System.out.print("\nEnter id of Flight to open: ");
          FlightID = sc.nextLine();
    
          Flight = getFlight(FlightID);
    
          if (Flight == null)
          {
             System.out.printf("\nError - Flight %s not found!\n", FlightID);
          }
          else
          {
             boolean result;
    		
    			result = Flight.open();
    		
             if (result == true)
             {
                System.out.printf("\nFlight %s opened for boarding.\n", FlightID);
             }
             else
             {
                System.out.printf(
                      "\nError Flight %s cannot be opened for boarding!\n", 
                      FlightID);
             }
          }  
          }
    The method is written in another class, and I'm using eclipse.
    I want it to be able to throw an exception if the flight cannot be opened, but i've tried putting it in but keeps erroring , so i'm a bit lost.

    Really appreciate it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Actually to be honest I don't know where to put the catch, that's the problem I'm having.
    I only know where to put the try
    Java Code:
     try {
     public static void openFlight()
       {
          Flight Flight;
          String FlightID;
    
          System.out.print("\nEnter id of Flight to open: ");
          FlightID = sc.nextLine();
    //etc...

  4. #4
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    the catch line I'm trying to put is something like

    Java Code:
     catch(FlightNotScheduled fnsE){System.out.println(fnsE.getMessage());

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by omgonoes View Post
    I only know where to put the try
    Java Code:
     try {
     public static void openFlight()
    .......
    Well, that isn't right either.

    Suggested reading : Catching and Handling Exceptions

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by omgonoes View Post
    Hi i'm having trouble doing the try catch in my methods.
    ...
    I want it to be able to throw an exception if the flight cannot be opened, but i've tried putting it in but keeps erroring , so i'm a bit lost.
    So which is it?
    Do you want to throw an exception when result is false, or do you need to catch an exception thrown by another method call (eg the open() method on Flight)?

Similar Threads

  1. catching exceptions (hierarchy)
    By kailash in forum Java Servlet
    Replies: 2
    Last Post: 02-10-2011, 06:43 AM
  2. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 02:06 AM
  3. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  4. Catching exception from another library (class)
    By ezilka in forum New To Java
    Replies: 15
    Last Post: 05-26-2010, 06:59 AM
  5. Replies: 2
    Last Post: 09-18-2009, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •