473,386 Members | 1,798 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,386 software developers and data experts.

catching exceptions

hi i need to catch and try exceptions , one for int and one for an array out of bounds i dont undertand how my code is'nt working for array out of bounds heres my code


import java.util.* ;

public class ExceptionHomework
{
Scanner scan ;
int data;
int slot;
int[] value;

/**
* Constructor for objects of class
* ExceptionHomework
*/
public ExceptionHomework()
{
scan = new Scanner( System.in );
data=0;
slot=0 ;
value = new int[10];
}



/**
* This is the method you must change by adding
* try-catch blocks
*
*/
public void sampleMethod() throws ArrayIndexOutOfBoundsException
{
System.out.print("Enter the data: ");
data = scan.nextInt();
System.out.print("Enter the array index: ");
slot = scan.nextInt();
value[slot] = data;
try{
for(slot =0;slot <10;slot++){
value[slot] = slot;
}
System.out.println(value);
}
catch(ArrayIndexOutOfBoundsException e){
//printed just to inform that we have entered the catch block
System.out.println("Oops, we went to far, better go back to 0!");
}
finally{
System.out.println(value);
//method call to continue program
}

}
}

heres my driver

public class ExceptionHomeworkDriver
{
public static void main ( String [] args)
{
ExceptionHomework exHmwk = new ExceptionHomework();
exHmwk.sampleMethod();
System.out.println("Good By");
}
}
Apr 25 '07 #1
5 1633
sandyw
122 100+
hi i need to catch and try exceptions , one for int and one for an array out of bounds i dont undertand how my code is'nt working for array out of bounds heres my code


import java.util.* ;

public class ExceptionHomework
{
Scanner scan ;
int data;
int slot;
int[] value;

/**
* Constructor for objects of class
* ExceptionHomework
*/
public ExceptionHomework()
{
scan = new Scanner( System.in );
data=0;
slot=0 ;
value = new int[10];
}



/**
* This is the method you must change by adding
* try-catch blocks
*
*/
public void sampleMethod() throws ArrayIndexOutOfBoundsException
{
System.out.print("Enter the data: ");
data = scan.nextInt();
System.out.print("Enter the array index: ");
slot = scan.nextInt();
value[slot] = data;
try{
for(slot =0;slot <10;slot++){
value[slot] = slot;
}
System.out.println(value);
}
catch(ArrayIndexOutOfBoundsException e){
//printed just to inform that we have entered the catch block
System.out.println("Oops, we went to far, better go back to 0!");
}
finally{
System.out.println(value);
//method call to continue program
}

}
}

heres my driver

public class ExceptionHomeworkDriver
{
public static void main ( String [] args)
{
ExceptionHomework exHmwk = new ExceptionHomework();
exHmwk.sampleMethod();
System.out.println("Good By");
}
}
I'm learn this myself...
Question what does the
1.
Expand|Select|Wrap|Line Numbers
  1. catch(ArrayIndexOutOfBoundsException e){
e stand for I think you want exception instead of e.
2. not sure you might need a return
finally{
System.out.println(value);
//method call to continue program
}
return //what int you are looking for
}

sandyw
Apr 25 '07 #2
changed some of the code but now it doesnt like my driver which i dont know why, error says unreported exception java.lang.exception must be caught


import java.util.* ;

public class ExceptionHomework
{
Scanner scan ;
int data;
int slot;
int[] value;

/**
* Constructor for objects of class
* ExceptionHomework
*/
public ExceptionHomework()
{
scan = new Scanner( System.in );
data=0;
slot=0 ;
value = new int[10];
}



/**
* This is the method you must change by adding
* try-catch blocks
*
*/
public void sampleMethod() throws Exception

{
try{
System.out.print("Enter the data: ");
data = scan.nextInt();
System.out.print("Enter the array index: ");
slot = scan.nextInt();
value[slot] = data;

}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array Index Out of Bounds");

}

catch (InputMismatchException e){
System.out.println ("Execption enter a number:" + e );
}

finally {
System.out.println(value);


}
}


}

my driver

public class ExceptionHomeworkDriver
{
public static void main ( String [] args)
{
ExceptionHomework exHmwk = new ExceptionHomework();
exHmwk.sampleMethod();
System.out.println("Good By");
}
}
Apr 25 '07 #3
r035198x
13,262 8TB
changed some of the code but now it doesnt like my driver which i dont know why, error says unreported exception java.lang.exception must be caught


import java.util.* ;

public class ExceptionHomework
{
Scanner scan ;
int data;
int slot;
int[] value;

/**
* Constructor for objects of class
* ExceptionHomework
*/
public ExceptionHomework()
{
scan = new Scanner( System.in );
data=0;
slot=0 ;
value = new int[10];
}



/**
* This is the method you must change by adding
* try-catch blocks
*
*/
public void sampleMethod() throws Exception

{
try{
System.out.print("Enter the data: ");
data = scan.nextInt();
System.out.print("Enter the array index: ");
slot = scan.nextInt();
value[slot] = data;

}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array Index Out of Bounds");

}

catch (InputMismatchException e){
System.out.println ("Execption enter a number:" + e );
}

finally {
System.out.println(value);


}
}


}

my driver

public class ExceptionHomeworkDriver
{
public static void main ( String [] args)
{
ExceptionHomework exHmwk = new ExceptionHomework();
exHmwk.sampleMethod();
System.out.println("Good By");
}
}
1.)Please use code tags when posting code.
2.) Read the error message. It is telling what you need to do. You need to add a catch clause that catches Exception.
Apr 25 '07 #4
sorry i dont know what code tags are

i added


catch (Throwable e) {
System.out.println ("exception");
}

and tried


catch (Exception e) {
System.out.println ("exception");
}

is this what you meant because it didnt work thank for replying tho
Apr 25 '07 #5
thanks everyone for your help but finally done it
Apr 25 '07 #6

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

Similar topics

1
by: Rolf | last post by:
I understand a compilation error occurs when a method that throws no exceptions is the only code in a try block. What I don't understnad is why I can specify the catching of an Exception for a...
2
by: Keith Bolton | last post by:
I am handling exceptions currently using try, except. Generally I don't handle specific exceptions and am catching all. Then if an exception occurs, I would like to capture that error string....
15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
8
by: Adam H. Peterson | last post by:
Hello, I sometimes find myself writing code something like this: try { Derived &d=dynamic_cast<Derived&>(b); d.do_something_complicated(); // etc.... } catch (std::bad_cast) { throw...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
12
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls...
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
5
by: Simon Tamman | last post by:
I have an object named DisasterRecovery. The Ctor of this object is this: private DisasterRecovery() { Application.ThreadException+= new...
12
by: Karlo Lozovina | last post by:
I'm not sure if Python can do this, and I can't find it on the web. So, here it goes: try: some_function() except SomeException: some_function2() some_function3() ...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.