Unreported Exception - Must Be Caught Or Declared To Be Thrown
Oct 4, 2014
I am getting the above error and am not sure which direction to proceed. In the class, the only way we have discussed getting user input is by System.in.read. I have searched and apparently found better methods for getting user input, but wanted to stick with what has been presented thus far.
Assignment Directions:
1. Create a new class named “valuemethod”
2. Create a new method named “Main”
3. In Main Write the code that will call the method EnterPay and YearlySal
4. Create a new method named “EnterPay”
5. In the EnterPay method Write the code that asks the user to input their hourly wage. Use the formula to calculate their yearly salary: wage * 2040. Return the yearly salary to the main method
6. In the main method write the code that will display this: “Your yearly salary is:
Java Code:
package valuemethod;
public class Valuemethod
{
public static void main(String[] args)
//throws java.io.IOException -- moved to EnterPay
[Code] .....
View Replies
ADVERTISEMENT
Apr 10, 2014
I want to write classes with methods that perform JDBC operations that throw SQL exceptions. For many of the methods, I'd ideally like to be able to have them catch exceptions and just send them to a standard Logging system "IF" the code that calls the methods is not going to catch the same exception. However, I'd like the "option" to have code that calls these methods catch the same errors if I want to but not "Require" the calling routines to catch them.... so I don't want to declare the methods with a "throws" that would require all calling code to Try/catch.
For some background, the logic behind what I'm looking to do is that there will be lots of places where these classes and their methods may be used where the code is basically "throw away" scripting code where just having error logs generated is more than sufficient. However there are also places I want to use the same classes/methods that I would want to handle the exception differently. So, for at least half the places I want to use these methods, there's no good reason to require cluttering the calling code with Try/catch, but when I DO want to handle the exceptions, I'd like them to get passed up to the calling routine so I can handle them in a way that is appropriate for the calling routine. Does that make sense?
I guess I'm kind of looking for is the ability to "override" the catch of a called method "IF" I want to but to treat the method as though it doesn't throw any exception "IF" I don't want to override the called routines catch logic.
Is this possible?
View Replies
View Related
Oct 10, 2014
need to navigate/render a jsp when a custom exception is thrown from a code block in my app.for that in web.xml I have configured the error handling
<error-page>
<exception-type>com.exampl.RecordNotFoundException</exception-type>
<location>/WEB-INF/errorpages/recordnotfound.jsp</location>
</error-page>
Code block
@RequestMapping(value = "/record", method = RequestMethod.GET)
public ModelAndView getPages() throws RecordNotFoundException{
throw new RecordNotFoundException("E888", "This is custom message X");
}
My questions are:
Is this the correct way to invoke a jsp when my code throws an exception?Do I need a try catch block to handle the thrown RecordNot Found Exception, if yes how would I navigate to the jsp from the catch block?
View Replies
View Related
Feb 10, 2014
I have been working with socket programming. I was trying to develop a two way communication from server to client. The data is being received when sent from server to client but there is a problem when I am sending from client to server.
I am getting an error as "Socket is closed" pointing to the line where I have created a BufferedReader. I have simplified some part of my code to be very specific.
Code attached.
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String args[])
{
String id="policeone";
int hashcodeid=0;
[Code] ....
Error::
Receiving string from client: 'java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:872)
at Server.main(Server.java:51)
View Replies
View Related
Jul 27, 2014
Below is a code of a simple file downloader in java. I want to detect an exception when the connection is broken in the middle of downloading a file. Now, I run the code and in the middle of downloading the file, i turn off my wifi with keyboard wifi off button. Doing this the program hangs forever without throwing any exceptions and without terminating. It seems it blocks forever.why is exception not thrown? Secondly, in the following code you can see this line //con.setReadTimeout(2000); which is currently commented out. Removing the comment and running the program, now if connection breaks in the middle by turning off wifi then it waits for 2 seconds and then if it cannot read then it terminates without throwing exception. So, again in this case why is it just terminating and not throwing any exception?
Java Code:
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.*;
[code]...
View Replies
View Related
Apr 21, 2014
I just wanna confirm that when a certain processing throws an exception even when said exception happens inside doFilter, any servlet container will never proceed to the next filter right?
View Replies
View Related
May 2, 2015
I am attempting to create a very simple game where two squares move around and each can see the other square moving. This is a multi-player game over a TCP network using JavaFX. I am getting an illegal state exception when I call requestFocus(). This is my server:
public class GameServer extends Application {
ObjectOutputStream p1out;
ObjectOutputStream p2out;
int PLAYER1 = 1;
int PLAYER2 = 2;
[Code] .....
View Replies
View Related
May 27, 2014
Sometimes a nullpointerexception is thrown, in some cases null is easily printed as a value of a variable without an exception being thrown.
View Replies
View Related
Aug 31, 2014
The 2 minute drill from page 69 SCJP kathy and bert book, says regarding Interfaces, that - "A legal nonabstract implementing class must not declare any new checked exceptions for an implementation method."
When I try the below given code in eclipse , it does not throw any errors . (Here I have tried to throw NullPointerException from testFunc whereas the interface function throws IllegalStateExc)
package abstracttesting;
public class StaticCheck implements check{
public void testFunc() throws NullPointerException{
// TODO Auto-generated method stub
} public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
interface check{
void testFunc() throws IllegalStateException;
}
View Replies
View Related
Oct 19, 2014
In my cs class, we have to write a program that throws an exception if the user enters a negative number, the program should prompt the user to enter only positive numbers and then let them retype the number. But everytime I throw an exception, my program gets suspended.
What am I doing wrong, or is there no way to continue a program if an exception is thrown? Does the program automatically get suspended when an exception is thrown?
try{
do
{
N = kb.nextDouble();
if(N<0) {
throw new NegativeArraySizeException();
}
else
j++;
}
while(j==0);
fill.size(N);
}
catch(NegativeArraySizeException e) {
System.out.println("The number you entered must be positive. Please try again.");
}
View Replies
View Related
Jul 9, 2014
I think the following code should trigger a compiler error.
public final class IPoint3D {
private double x;
private double y;
private double z;
public IPoint3D(double x, double y, double z)
[Code] .....
View Replies
View Related
Apr 20, 2015
Basically I'm trying to compute the standard deviation for a set of numbers and I'm using previously know data to compute this. The data I'm working with is here (note all of this is fake as its for an assignment):
Id,First Name,Last Name,Quiz1,Quiz2,Quiz3,Lab1,Lab2,Assignment1,Exam1,Exam2
58136,Wade,Andrews,5,8,2,2,20,47,20,40
59759,Jacqueline,Simmons,9,4,1,16,13,2,82,41
28056,Frederick,Castro,8,0,6,3,6,29,53,63
15532,Annette,Myers,2,6,0,14,13,4,77,43
Next this is the code that I know is producing the error:
/**
* Pass in original data from files, and pass in computed grades as arraylist. this returns all of the standard deviations to be printed
*/
public static ArrayList calcStandDev(String data1[], ArrayList<Double> grades, ArrayList<Integer> averages)
{
double standDev = 0;
int j = 0;
//gets rid of first element in string array, makes processing MUCH easier
String[] data = Arrays.copyOfRange(data1, 1, data1.length);
[Code] ....
Look for the line with all the ***'s, it is producing the "java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer".
The reason I dont undersand why its producing this error is because temp.get(i) should be a integer - averages.get(i) (is type double), I do believe automatic up casting is present here making them both a double then I multiply it by itself producing a double? I dont understand where the casing is occurring. And just incase you want the formula I'm using for standard deviation here it is:
((x1 - ave)^2 + (x2 - ave) ^2 + (xn - ave) ^ 2) / (n-1)
then squareroot above
Why I'm getting this error! I tried changing a few dataTypes but I just cant avoid exceptions being thrown everywhere .
View Replies
View Related
Dec 29, 2014
Being new to java I am a bit lost as to why my session attribute for this banking app wont add the deposits to the session var... it just keeps going back to the amount I set it to originally - so if I set the beginning balance to 3000 then deposit 100 it becomes 3100, but if I then try deposit another amount eg. a extra 200 it becomes 3200 not 3300 like it should be !!
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.DecimalFormat;
public class SessionBank extends HttpServlet
[Code] .....
View Replies
View Related
Jan 7, 2014
I have come across some code where the method signature is like,
public methodName(final String argument) {
...
}
I fail to grasp why final is needed here. Is it only because of the field immutabilty? What big picture am I missing in this case?
View Replies
View Related
Jun 3, 2014
From the tutorial:instance variables can be declared in class level before or after use.But the code below does not compile. Why?
Java Code:
public class MainApp {
i=5;
public static void main(String[] args) {
System.out.println("Hello World");
}
int i;
} mh_sh_highlight_all('java');
View Replies
View Related
Sep 12, 2014
I need to create a simply application that would display the results of two dice thrown five times and the total of those results. This is shown below in the attached file.
The problem is, I have a do-while loop that loops 6 times. Inside the loop, I have 2 random.nextInt(5) that generate random numbers. But how can I output the total? How can I make a variable equal to the sum of the two random numbers if the two random numbers are located inside a do-while loop?
Attached below is also the code I have thus far.
(Attached below is both files: what it needs to look like, and what it currently looks like)
View Replies
View Related
Jun 25, 2014
public class Ball {
private int a=show();
int b;
Ball()
{
b=20;
}
public static void main(String args[])throws Exception {
System.out.println(new Ball().a);
}
private int show()
{System.out.println(b);
return b;
}
}
I wanted to know when are the objects created ? when are they initialized? and how is show() getting called without any reference or "this" ?
View Replies
View Related
Dec 1, 2014
While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,
Q1) My question is Why they declared the List as interface rather than Abstract class?
Q2) i read some site -
List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?
Answer required with detailed information for Q1 and Q2.
View Replies
View Related
Jan 24, 2014
I was giving a quick skim to some tutorials on the internet where I have found the exception that is handled is also declared in the throws clause of the method wrapping the try-catch block. For a code representation consider the following:
public void methodName() throws IOException {
try {
...
} catch (IOException ex) {
.... TODO handle exception
}
}
View Replies
View Related
Jun 9, 2014
A method is declared to take three arguments. A program calls this method and passes only two arguments.Will it take the third argument as
1 null
2 void
3 zero
4 Compilation error
View Replies
View Related
Sep 29, 2014
Right, so I got this program. It takes input from the user and assigns it to fields on an object. But, it's meant to check the users input. If the user enters bad input, it's supposed to throw this exception. For each of these exceptions, theres a class specifically for it.
public class PayrollDemo
{
public static void main(String[] args)
{
Payroll pr = new Payroll ("Test Name", 234);
System.out.println("Current Employee Information.
");
System.out.println("Name: " + pr.getName());
System.out.println("ID: " + pr.getID());
System.out.println("Hourly Pay Rate: " + pr.getHourlyPayRate());
[Code] ....
And this is the exception class.
public class InvalidNameException extends Exception
{
/**
No-arg constructor
*/
public InvalidNameException()
{
super("Invalid name");
}
}
PayrollDemo.java:43: error: cannot find symbol
InvalidNameException.InvalidNameException();
^
symbol: method InvalidNameException()
location: class InvalidNameException
1 error
It's just meant to tell the user that they entered an invalid value, which would mean if they entered an empty string instead of a name.
View Replies
View Related
Mar 5, 2015
How do you declare methods for a class within the class whilst objects of the class are declared else where?
Say for instance, I have a main class Wall, and another class called Clock, and because they are both GUI based, I want to put a Clock on the Wall, so I have declared an instance object of Clock in the Wall class (Wall extends JFrame, and Clock extends JPanel).
I now want to have methods such as setClock, resetClock in the Clock class, but im having trouble in being able to refer to the Clock object thats been declared in the Wall class.
Is this possible? Or am I trying to do something thats not possible? Or maybe I've missed something really obvious?
View Replies
View Related
Jul 27, 2013
Take this:
class test
{
static int i=j;
static int j=10;
.....
this will give illegal forward reference ....
but this will compile successfully ..
class test
{
static int i=test1();
static test1()
{
return 20;
}
}
.....
plz assume we have main method in both cases ..
java would be loading all static members first and would be assigning default values .. and then will be running all the initializers from to bottom ..Why second case is a compile success and not first .. as in second also test1 method is declared after its usage ..
View Replies
View Related
Jan 4, 2014
I have a program which runs perfectly fine in Eclipse. however, the same program after exporting to jar in desktop and then executing in cmd gives exception.
why is this difference.
in eclipse. it is built up 1.6 JDK and run using 1.6 JRE.
in CMD, it runs by 1.7 JRE.
can this JRE version make a difference. Technically should not i believe.
View Replies
View Related
Mar 15, 2014
value1 = text1.getText();
value2 = text2.getText();
Connect c = new Connect(value1,value2);
if(c.check()== true)
{
Menu start = new Menu(c.getID());
dispose();
[code].....
I get an error "unreported exception java.SQLException; must be caught or declared to be thrown" how can i solve this I have taken a few basic classes but never got to exceptions.
View Replies
View Related
Oct 16, 2014
I want to encrypt and decrypt a txt file by using PGP.I completed my PGP Encryption.I'm getting an error in PGP Decryption. The error msg as follows
Quote:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid PGP message
at com.newpackage.bouncy.PGPUtils.getDataList(PGPUtil s.java:502)
at com.newpackage.bouncy.PGPUtils.decryptFile(PGPUtil s.java:198)
at com.newpackage.bouncy.PGPFileProcessor.decrypt(PGP FileProcessor.java:56)
at com.newpackage.bouncy.Tester.testDecrypt(Tester.ja va:46)
at com.newpackage.bouncy.Tester.main(Tester.java:61)
I am getting this error in the following line of my code.
PGPObjectFactory factory=new PGPObjectFactory(PGPUtil.getDecoderStream(is));
=>> Object obj=factory.nextObject();//here i am getting obj as null
if (obj == null) {
//System.out.println("Invalid PGP Message");
//System.exit(0);
[Code] .....
View Replies
View Related