473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Unreachable statement?? -- Error code help!

2
Hi! I'm not that good at programming, so I was wondering if anyone could tell me why I'm getting an unreachable statement error for lines 18, 35, 68, and 86.

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2.  
  3. public class Graph {
  4.  
  5.     ArrayList<ArrayList<Integer>> graph;
  6.  
  7.     public void Graph( int n )
  8.     {
  9.         graph = new ArrayList<ArrayList<Integer>>(n);
  10.     }
  11.  
  12.     public boolean connect( int add1, int add2 )
  13.     {
  14.         if( add1 >= graph.size() || add2 >= graph.size() 
  15.                 || add1 == add2 )
  16.         {
  17.             throw new IllegalArgumentException();
  18.             return false;
  19.         } else if( graph.get(add1).contains( new Integer(add2) ) ||
  20.                graph.get(add2).contains( new Integer(add1) ) )
  21.         {
  22.             return false;
  23.         } else {
  24.             graph.get(add1).add( new Integer(add2) );
  25.             graph.get(add2).add( new Integer(add1) );
  26.             return true;
  27.         }
  28.     }
  29.  
  30.     public boolean disconnect( int dis1, int dis2 )
  31.     {
  32.         if( dis1 >= graph.size() || dis2 >= graph.size() )
  33.         {
  34.             throw new IllegalArgumentException();
  35.             return false;
  36.         } else if( !graph.get(dis1).contains( new Integer(dis2) ) ||
  37.                !graph.get(dis2).contains( new Integer(dis1) ) )
  38.         {
  39.             return false;
  40.         } else {
  41.             graph.get(dis1).remove( new Integer(dis2) );
  42.             graph.get(dis2).remove( new Integer(dis1) );
  43.             return true;
  44.         }
  45.     }
  46.  
  47.     public boolean areConnected( int adj1, int adj2 )
  48.     {
  49.         if( adj1 >= graph.size() || adj2 >= graph.size() )
  50.         {
  51.             throw new IllegalArgumentException();
  52.         }
  53.  
  54.         if( graph.get(adj1).contains( new Integer(adj2) ) &&
  55.             graph.get(adj2).contains( new Integer(adj1) ) )
  56.         {
  57.             return true;
  58.         } else {
  59.             return false;
  60.         }
  61.     }
  62.  
  63.     public ArrayList<Integer> adjacencies( int node )
  64.     {
  65.         if( node >= graph.size() )
  66.         {
  67.             throw new IllegalArgumentException();
  68.             return null;
  69.         }
  70.  
  71.         ArrayList<Integer> hold = new ArrayList<Integer>(graph.size());
  72.  
  73.         for( int i = 0; i > graph.get(node).size(); i++ )
  74.         {
  75.             hold.set(i,graph.get(node).get(i));
  76.         }
  77.  
  78.         return hold;
  79.     }
  80.  
  81.     public int degree( int node )
  82.     {
  83.         if( node >= graph.size() )
  84.         {
  85.             throw new IllegalArgumentException();
  86.             return 0;
  87.         }
  88.  
  89.         return graph.get(node).size();
  90.     }
  91.  
  92.     public boolean isEmpty()
  93.     {
  94.         return graph.size() == 0;
  95.     }
  96. }
  97.  


Any input is greatly appreciated!
Aug 26 '08 #1
4 6149
BigDaddyLH
1,216 Expert 1GB
Take a look at lines 17 and 18:

Expand|Select|Wrap|Line Numbers
  1. throw new IllegalArgumentException();//17
  2. return false;//18
Line 18 will never be executed because line 17 throws an exception, so 18 is unreachable. Solution: delete the unneeded line 18!
Aug 26 '08 #2
Nepomuk
3,112 Expert 2GB
Take a look at lines 17 and 18:

Expand|Select|Wrap|Line Numbers
  1. throw new IllegalArgumentException();//17
  2. return false;//18
Line 18 will never be executed because line 17 throws an exception, so 18 is unreachable. Solution: delete the unneeded line 18!
Same for the other lines - throwing an Exception leaves the current function just like return would.

Instead, you'll want to do something like this:
Expand|Select|Wrap|Line Numbers
  1. public void something()
  2. {
  3.    ...
  4.    boolean i;
  5.    try {
  6.       i = myFunction();
  7.    } catch(WhateverException we) {
  8.       i = false;
  9.    }
  10.    ...
  11. }
  12.  
  13. public boolean myFunction() throws WhateverException
  14. {
  15.    ...
  16.    if(error) throw new WhateverException;
  17.    else return true;
  18. }
Greetings,
Nepomuk
Aug 27 '08 #3
EmilyA
2
Thank you so much!!!!
Aug 27 '08 #4
Nepomuk
3,112 Expert 2GB
Thank you so much!!!!
You're welcome, EmilyA! And as we're at this anyway, welcome to bytes.com!

It's great to have you here!

When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags.

Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

Greetings,
Nepomuk
Aug 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: leza | last post by:
I am trying to understand why "continue" keyword is used for, therefore I compiled this code but I got this error message.. why ==================== C:\Program Files\JCreator...
1
by: Kepler | last post by:
I have a situation where I need to use a Using statement that creates some records in a database. After that completes, if it completes, I need to do some file creation. Any code I'm putting...
4
by: SteadySteps | last post by:
Hi I migrated a project which compiles correctly on VC 6.0 to VS 2002. However now all I get several warning that all the statements within catch blocks are "unreachable code". How can I correct...
2
by: Hovik Melikyan | last post by:
This code produces a 'unreachable code' warning at line 16 (throw new X ...) with no visible reason: #include <string> class X { std::string msg;
4
by: Gernot Frisch | last post by:
I get informed that parts of my code cannot be reached. Did I do something wrong? try { if (login(progstring) > 0) { return 1; // OK } }
3
by: cie | last post by:
Hi, anyone please help me when I try to close a connection to the database from a static method, "Unreachable code detected" error occured. What's the problem actually? Thank you before
2
by: judge82 | last post by:
i keep getting that error on line 139. i need help please * RegLoginServletHW3.java * * Created on October 17, 2007, 10:29 PM */ package ITIS5166;
1
by: HillBilly | last post by:
What does that error mean and why would each break statement be marked as unreachable? It implies the return will --always-- return some --thing-- if even null and no further processing can...
2
by: selasebytes | last post by:
how do i get over this warning to get my program running correctly?? Below is the faulty part of the code. Am trying to calculate discount on a purchase form. the codes for my discount is nested in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.