Try Catch Passing Parameter
Oct 15, 2014
I'm asking if there is a way to pass parameter between try/catch block.
I have a method:
public void invia(OiStorto invioaca) throws Exception {
Long id=0L;
try {
//some code
for (VElenpere elenpere : elenperes) {
[Code] ...
The method called in the for:
private void popolaScompiute(Long anno, InviaEOI inviaEOI0, _4IntegerType param, ElOpeIncType opereIncompiute,
VElencoOpere elencoOpere,Long id) {
try {
//some code
for (OiOpera opere : oiOpere){
[Code] ....
And finally the method in which the error can occur (the method poputa is called for every id)
private void poputa(ElOpeIncType opereIncompiute, OpeIncType operaSingola, OiOpera opere) {
try {
//some code
} catch (NullPointerException e) {
e.printStackTrace();
//return id;
}
So method invia call the method popolaScompiute, inside popolaScompiute there is an iteration through some id and for some id can occur an error; what i want is the getting the value of id in the first method invia, using the block try/catch. Is there a way to accomplish this?
View Replies
ADVERTISEMENT
Oct 14, 2014
Any way to pass parameter between try/catch block.
In other word:
I have a method:
public void invia(OiStorto invioaca) throws Exception {
Long id=0L;
try {
//some code
for (VElenpere elenpere : elenperes) {
popolaScompiute(anno, inviaEOI0, param, opempiute, elenpere,id);
[Code] ....
And finally the method in which the error can occur (the method poputa is called for every id)
private void poputa(ElOpeIncType opereIncompiute, OpeIncType operaSingola, OiOpera opere) {
try {
//some code
} catch (NullPointerException e) {
e.printStackTrace();
//return id;
}
So method invia call the method popolaScompiute, inside popolaScompiute there is an iteraction through some id and for some id can occur an error; what i want is the getting the value of id in the first method invia, using the block try/catch. Is there a way to accomplish this?
View Replies
View Related
Apr 30, 2014
I'm using Java in BlueJ where I'm trying to write a class to store cycling club membership details in a map. I'm trying to pass a map as a parameter to a function that will iterate over the map and print out the member details. My code is:
public class CtcMembers
{
//instance variables
private Map<String, Set<String>> memberMap;
/**
* Constructor for objects of class CtcMembers
[Code] ....
When I execute the following in the OU workspace:
CtcMembers c = new CtcMembers();
c.addCyclists();
I can see a map populated with the expected details.
When I execute the following in the OU workspace:
c.printMap(c.getMap());
I get the following error:
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
in CtcMembers.printMap(CtcMembers.java:81)
in (OUWorkspace:1)
I wasn't aware I was trying to cast anything so this has got me baffled.
View Replies
View Related
Aug 8, 2014
i want to pass an object of type Software to assign it to a computer from Computer class...
i should note that computer and software are arrays of objects thats the variable and method to set software in Computer class
private Software[] software;
public void setSoftware(Software software,int index){
this.software[index]=software;}
note: the user choses the a computer from a list and a software as will
for example the program will show the user 2 computers
0 for computer: apple, Model: mac mini, Speed: 2.8
1 for computer: sony, Model: vaio, Speed: 2.2
the user enters the index he wants then the program will show a list of software to add to the computer selected
the error I'm having is run time error Exception in thread "main" java.lang.NullPointerException and it points to these 2 lines
1.comp[Cch].setSoftware(software,Sch);
2. the method setSoftware
every thing is running correctly but this step above
Cch= the chosen computer index
Sch= the chosen Software index
why am i getting an error and how to fix it?
View Replies
View Related
May 14, 2014
I am trying to run an example in which I pass a parameter to imported file but its not working.
This is my header.jspf file which I will include in a jsp file.
<img >
<br>${param.sub}
And This is the JSP file contact.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:import url="header.jspf">
<c:param name="sub" value="We love to hear from our clients------------" />
</c:import>
<br><br>
Now this is JSP page content
</body>
</html>
When I hit the contact.jsp file , It does show me the image I have on header.jspf , but its not able to print the parameter that I am setting in <c:param>
So after showing image it just shows ${param.sub}. So EL is not translated properly.
View Replies
View Related
Jan 30, 2015
Java SE Runtime Environment build 1.8.0..This is part of the code:
public static int addAddress (String[] number, boolean[] front, double[] total) {
int num = 0;
double ffee = 0;
/*boolean value = false;*/
[code]...
I have tried using the line of code commented out, /*boolean value = false;*/. However, another error is displayed. The compiler shows the following...
Inspection.java:33: error: incompatible types: boolean cannot be converted to boolean[]
front[num]= defineFront(num, value);
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output error...I know that boolean values are by default stored as false, once you create the array. However, I'm having trouble passing the variable to the method.
View Replies
View Related
Mar 8, 2015
I have an object that has an instance of another object class as its parameter :
CombinationLock oneHundred = new CombinationLock(28,17,39);
Locker Mickey = new Locker(100, "Mickey", 3, oneHundred);
This is for a locker, which has a combination assigned to the student. Within the locker class I have the following constructor:
public Locker(int locker, String student, int numberOfBooks, CombinationLock combo) {
this.locker = locker;
this.combo = combo;
this.student = student;
this.numberOfBooks = numberOfBooks;
}
combo is the private CombinationLocker object I created within the Locker class. Do I need to pass the combo object on to the CombinationLock class? For reason, I do not comprehend, the combination password from the main class is not passing through to the CombinationLock class, and the combination values are all zero.
View Replies
View Related
Feb 23, 2014
I am trying to make a class definition file for an ASCII File.
Ultimately, I want to be able to add methods to allow the image produced by the file to be printed normally, then printed with various manipulations.
However, for some reason, whenever I try to run the program to test my normalPrint method, it terminates without printing anything.
I think this is because the array's values width and height are not within the scope of the method. I tried passing the array as a parameter for the method like so:
Java Code:
public void normalPrint(char [][] poop){
//method here
} mh_sh_highlight_all('java');
But it gave me an error that stated
"The method normalPrint(char[][]) in the type asciiFile is not applicable for the arguments ()"
Class Definition:
Java Code:
import java.io.*;
import java.util.Scanner;
public class asciiFile {
int height;
int width;
[Code] .....
View Replies
View Related
Jan 15, 2014
There is a method taken from a class with several try and catch blocks. If you think it is possible, add one more catch block to the code to catch all possible exceptions, otherwise say 'Not possible' with your reason.
public String getCountyCode(Address inputAddress) throws AddressNormalizationException
{
String retval = null;
try
{
retval = this.normalizeAddress(inputAddress).getCountyCode( );
}
catch(InvalidAddressException e)
[code]...
View Replies
View Related
Dec 13, 2014
Assuming that we have two classes B and C which inherit from class A. What is the best way to pass a parameter from an object of class B to an object of class C by the use of class A without using static variable and without defining a get function in B?
View Replies
View Related
Jun 9, 2014
I wrote a program using switchcase.I used do while to show the menu to the user until the user decides to exit the menu.I used try catch to prevent ant exception and it worked properly.But i got one problem.When exception occurs,desired msg is printed but i am unable to display the menu to the user.So user wont be able to continue after an exception is caused.
View Replies
View Related
Feb 3, 2015
class Arg
{
public void rt()throws Exception
{
System.out.println("hi");
throw new RuntimeException();
}
public static void main(String args[])
[code]...
the way to handle the exception that has been in the catch block{b.rt()} .
View Replies
View Related
Nov 7, 2014
public class ThrowException {
public static void main (String[] args) {
var x=prompt("Enter a number between 0 and 10:","");
try {
if (x>10){
throw "Err1";
} else if (x<0){
throw "Err2";
} else if (isNaN(x)){
throw "Err3";
}
}
catch(er){
[code]...
It's telling me where catch(er) is: <identifier> expected..I've watched videos, but no one seems to encounter this error....am I missing a segment of code?
View Replies
View Related
Sep 19, 2014
In the following piece of code Iam confused as to where the InputMismatchException in the catch block is thrown on the first place? Is the InputMismatchException thrown automatically with declaring to throw the exception?
import java.util.*;
public class InputMismatchExceptionDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueInput = true;
[code]....
View Replies
View Related
Apr 13, 2015
Is it a best practice to return from try block or place return statement after try-catch when we intend to return a value from a method(* Catch block is being also used to rethrow the exception)??
View Replies
View Related
Feb 18, 2015
Regarding return statements within methods. So I have a method containing try and catch block (as required) and much like when you have an if else statement... I noted you have to return an object for both the try and catch blocks. Now in my case my method should return a List object.
The way I have tried to overcome this:
- I've initialised a List object to null as an attribute of the class I'm working in.
- Therefore in the catch block would just simply return the null List object, where as the try block would return the non-empty List (which is what I want).
- I then just test to see if the List != null, when the method is invoked... and that is that.
However the method always seems to return null (when it shouldn't).
View Replies
View Related
Apr 27, 2014
I've been assigned to write a program that will convert binary to decimal that uses the try/catch block. In the program that I have written, I was wondering if it is possible to write an addition catch statement that will present an error if any number other than a 0 or 1 is entered by the user. I have already done this in the binaryToDecimal method, but I am just messing around to see if it is, in fact, possible.
Java Code:
import java.io.IOException;
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
[Code] .....
View Replies
View Related
May 5, 2014
So I'm pretty sure this is correct, as it follows most examples I can find online, but I keep getting an error that my return variable cannot be resolved. The error is on the return conn; statement. It says conn cannot be resolved. If I place it above within the try block it allows it but then I receive an error saying the method getDBConnection must return type Connection.
import java.sql.*;
public Connection getDBConnection() {
try {
Class.forName("org.sqlite.JDBC");
String path = "jdbc:sqlite::resource:project.db";
[Code] .....
I don't want to create this method. Basically I want to connect to the database in the main program, but I do want methods that can access the DB too. But however I place it, it doesn't let me touch any of the DB variables outside of the Try block.
View Replies
View Related
Jan 29, 2015
I want to catch the exception in my program is the below way:
class Temp
{
public static void main(String s[])
{
try
{
int x=10/s.length;
System.out.println(x);
[Code] ....
I am expecting my program should give output as "java.lang.ArithmeticException: / by zero" but is giving
Temp.java:11: error: ')' expected
catch (ArithmeticException e | ArrayIndexOutOfBoundsException e)
^
Temp.java:11: error: ';' expected
catch (ArithmeticException e | ArrayIndexOutOfBoundsException e)
^
Temp.java:16: error: 'catch' without 'try'
catch (Exception e)
^
Temp.java:22: error: reached end of file while parsing
}
^
4 error
View Replies
View Related
Apr 19, 2014
I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:
public PairOfDice(int val1, int val2) {
// Constructor. Creates a pair of dice that
// are initially showing the values val1 and val2.
die1 = val1; // Assign specified values
die2 = val2; // to the instance variables.
}
public PairOfDice() {
// Constructor that calls two parameter constructor
}
I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.
Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.
View Replies
View Related
Jul 2, 2014
consider this program :
public class hello {
/**
* @param args
*/
public static void main(String[] args) {
int s = new hello().h();
System.out.println(s);
} public int h(){
try{
int g = 10/0;
[Code] .....
the output is 7. how the flow is working. i understand that there is a divide by zero exception after which the control goes to catch. what about the return statement in catch . why is it overridden by finally..........
View Replies
View Related
Dec 8, 2014
I'm trying to place a try/catch into a Actionlistener that will detect other characters other than alpha.
private class printbuttonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Object src = namesinput;
//String message = "Insert Message Here";
notesinput.setText("");
JOptionPane.showMessageDialog(null, notesinput, "Receipt",
JOptionPane.PLAIN_MESSAGE);
}
I'm not even sure if I'm trying to place it in the correct area in the code. However I like to perform this prior to the receipt being displayed so if there a issue the user can correct this before the final receipt has been sent .......
View Replies
View Related
Jul 16, 2014
I came across a code where the exceptions can be thrown from catch and finally block too. I never gave a thought on what scenarios that can be required. Some practical examples when/where it can be required to throw the exception from catch and finally blocks.
View Replies
View Related
Feb 28, 2015
I want to use a try catch block, but I am not sure how to fix this problem:
int a;
try{
a = Integer.parseInt(A.getText());
}
catch (Exception e){
Output1.setText("Error");
}
//do someting with a here
The purpose of the try-catch is to catch blank input.The problem with this is that underneath the try - catch I get an error saying that the variable might not have been initialized. I know why this happens. I know I could initialize the varaible before the try - catch, but there is no default or null I can set an int as. If I initialized it as 0, the blank input will no longer be catched.how to make this problem disappear?
View Replies
View Related
Jan 22, 2014
Right now we are learning about arrays and using the try/catch. Code below, I am trying to just display information about buildings. The application is good but not with the try and catch statement. I'm trying to just display the message of "please enter a building number" when a user puts a letter instead of a number(InputMismatchException) and then the user would have to put in one of the numbers. But when it runs and i put in a letter, it reads the message, but it always outputs the first building information ...
package username;
import java.util.InputMismatchException;
import java.util.Scanner;
//TallBuildings
public class TallBuildings {
//compare heights of buildings
[Code] .....
View Replies
View Related
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