» »

[java]problem z Throws

[java]problem z Throws

killa bee ::

public class Washer {
  Laundry laundry = new Laundry();
  public void foo() throws ClothingException {
  laundry.doLaundry();
}

public static void main (String[] args) throws ClothingException {
  Washer a = new Washer();
  a.foo();
}
}


Kaj bi se zgodilo v primeru da doLaundry() throws ClothingException ?

roko granata ::

V knjigi iz katere je vzet primer (celo na isti starni), ti pod četrto točko piše: "The JVM shuts down".

Če ne verjameš, lahko in je tudi zaželjeno, sprobaš :)

killa bee ::

kaj pa to pomeni "The JVM shuts down"?

Spura ::

JVM exita s kodo 143

roko granata ::

A bereš od konca proti začetku? :)

JVM - Java Virtual Machine; okolje v katerem se izvajajo Java procesi.

Za bolj specifičen opis pogooglaj JVM ali pa preberi prvo/drugo poglavje.

Na kratko - program se ti sesuje.

killa bee ::

jst vem kaj pomeni JVM ampak ne vem kaj pomeni "The JVM shuts down".

sm naredil programček in mi napiše:
Exception in thread "main" ClothingException
	at Washer.foo(Washer.java:15)
	at Washer.main(Washer.java:20)


a to se torej zgodi?

Zgodovina sprememb…

Wrop ::

Če nikjer v programu ne zajemaš izjem, potem zate to naredi JVM.
Lahko si zamišliš tako, da JVM pokliče tvojo main metodo v try catch stavku.

killa bee ::

kako veš če se je JVM ugasnil?

Wrop ::

pogledaš pod task manager, če obstaja še jvm proces.

killa bee ::

kaj pa v Linuxu

killa bee ::

Men to napiše, a to pomen da se je JVM ugasnil?
Exception in thread "main" ClothingException
    at Washer.foo(Washer.java:15)
    at Washer.main(Washer.java:20)

kunigunda ::

killa bee je izjavil:

kaj pa v Linuxu

ps -ef|grep java

Ce mas pa pot nastimano do jave, pa lohk tud

jps

Zgodovina sprememb…

killa bee ::

kako pa vem če mam pot do jave naštimano?

kunigunda ::

Mogoce je pa bolje za zacetek, da se vprasas ali imas pot nastimano ali ne, in kako to preveriti, predno se lotis programiranja :)
Google is your friend.

killa bee ::

jst mislilm da imam pot naštimano ker se postavim v datoteko kjer se xyz.java file nahaja ter ga kompajlam z ukazom javac xyz.java. Nisem posebno nastavljal poti.

Zgodovina sprememb…

kunigunda ::

Pol pa enostavno pozeni ukaz 'jps' pa bos videl ce ti jav error al ti pa izpise jvm procese

killa bee ::

Zaženem svoj testni program in mi najrej izpiše
Exception in thread "main" ClothingException
	at Washer.foo(Washer.java:15)
	at Washer.main(Washer.java:20)

po tem upišem
jps

pa mi izpiše
6398 Jps

A to pomeni da se je JVM ugasnil?

Zgodovina sprememb…

kunigunda ::

Ja, samo "samega sebe" je nasel :)

Sicer pa ponavadi main metodo ne throw-as.

public static void main (String[] args) {
Washer a = new Washer();
try {
a.foo();
} catch (Exception e) {
System.err.println("Crknu zaradi " + e.getMessage());
}
}

Zgodovina sprememb…

killa bee ::

ja a potem se je ugasnil al ne?

mislm JVM

Zgodovina sprememb…

kunigunda ::

da

killa bee ::

kako pa vem da se je ugasnil?

a zato ker piše 6398 Jps

kr?en ::

http://docs.oracle.com/javase/tutorial/...

Pa si malo preberi, da ne bos spraseval bedarij.

Pa ce bi znal uporabljat google/stackoverflow, bi bilo tudi zelo super.

killa bee ::

a mogoče kdo ve zakaj je spodnjih 5,6,7... zvokov popolnoma enakih?

import java.awt.*;
import javax.swing.*;
import javax.sound.midi.*;
import java.util.*;
import java.awt.event.*;

public class BeatBox {
	
	JPanel mainPanel;
	ArrayList<JCheckBox> checkboxList;
	Sequencer sequencer;
	Sequence sequence;
	Track track;
	JFrame theFrame;

	String[] instrumentNames = {"Bass Drum", "Closed Hi-Hat",
	"Open Hi-Hat","Acoustic Snare", "Crash Cymbal", "Hand Clap",
	"High Tom", "Hi Bongo", "Maracas", "Whistle", "Low Conga",
	"Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo",
	"Open Hi Conga"};

	int[] instruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
	
	public static void main (String[] args) {
		new BeatBox().buildGUI();
	}
	
	public void buildGUI() {
		theFrame = new JFrame("Cyber BeatBox");
		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		BorderLayout layout = new BorderLayout();
		JPanel background = new JPanel(layout);
		background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		checkboxList = new ArrayList<JCheckBox>();
		Box buttonBox = new Box(BoxLayout.Y_AXIS);
		
		JButton start = new JButton("Start");
		start.addActionListener(new MyStartListener());
		buttonBox.add(start);
		
		JButton stop = new JButton("Stop");
		stop.addActionListener(new MyStopListener());
		buttonBox.add(stop);
		
		JButton upTempo = new JButton("Tempo Up");
		upTempo.addActionListener(new MyUpTempoListener());
		buttonBox.add(upTempo);
		
		JButton downTempo = new JButton("Tempo Down");
		
		downTempo.addActionListener(new MyDownTempoListener());
		buttonBox.add(downTempo);
		
		Box nameBox = new Box(BoxLayout.Y_AXIS);
		for (int i = 0; i < 16; i++) {
			nameBox.add(new Label(instrumentNames[i]));
		}
		
		background.add(BorderLayout.EAST, buttonBox);
		background.add(BorderLayout.WEST, nameBox);
		
		theFrame.getContentPane().add(background);
		
		GridLayout grid = new GridLayout(16,16);
		grid.setVgap(1);
		grid.setHgap(2);
		mainPanel = new JPanel(grid);
		background.add(BorderLayout.CENTER, mainPanel);
		
		for (int i = 0; i < 256; i++) {
			JCheckBox c = new JCheckBox();
			c.setSelected(false);
			checkboxList.add(c);
			mainPanel.add(c);
		} // end loop
		
		setUpMidi();
		
		theFrame.setBounds(50,50,300,300);
		theFrame.pack();
		theFrame.setVisible(true);
		} // close method
		
		public void setUpMidi() {
			try {
				sequencer = MidiSystem.getSequencer();
				sequencer.open();
				sequence = new Sequence(Sequence.PPQ,4);
				track = sequence.createTrack();
				sequencer.setTempoInBPM(120);
		
				} catch(Exception e) {e.printStackTrace();}
		} // close method
		
		public void buildTrackAndStart() {
			int[] trackList = null;
			
			sequence.deleteTrack(track);
			track = sequence.createTrack();
		
			for (int i = 0; i < 16; i++) {
				trackList = new int[16];
				
				int key = instruments[i];
				
				for (int j = 0; j < 16; j++ ) {
					
					JCheckBox jc = (JCheckBox) checkboxList.get(j + (16*i));
					if ( jc.isSelected()) {
						trackList[j] = key;
					} else {
						trackList[j] = 0;
					}
				} // close inner loop
				
				makeTracks(trackList);
				track.add(makeEvent(176,1,127,0,16));
			} // close outer
			
			track.add(makeEvent(192,9,1,0,15));
			try {
				sequencer.setSequence(sequence);
				sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
				sequencer.start();
				sequencer.setTempoInBPM(120);
			} catch(Exception e) {e.printStackTrace();}
		} // close buildTrackAndStart method
				
		public class MyStartListener implements ActionListener {
			public void actionPerformed(ActionEvent a) {
				buildTrackAndStart();
			}
		} // close inner class	
		
		public class MyStopListener implements ActionListener {
			public void actionPerformed(ActionEvent a) {
				sequencer.stop();
			}
		} // close inner class	
		
		public class MyUpTempoListener implements ActionListener {
			public void actionPerformed(ActionEvent a) {
				float tempoFactor = sequencer.getTempoFactor();
				sequencer.setTempoFactor((float)(tempoFactor * 1.03));
			}
		} // close inner class
		
		public class MyDownTempoListener implements ActionListener {
			public void actionPerformed(ActionEvent a) {
				float tempoFactor = sequencer.getTempoFactor();
				sequencer.setTempoFactor((float)(tempoFactor * .97));
			}
		} // close inner class
		
		
		
		public void makeTracks(int[] list) {
			
			for (int i = 0; i < 16; i++) {
				int key = list[i];
			
				if (key != 0) {
					track.add(makeEvent(144,9,key, 100, i));
					track.add(makeEvent(128,9,key, 100, i+1));
				}
			}
		}
		
		public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
			MidiEvent event = null;
			try {
				ShortMessage a = new ShortMessage();
				a.setMessage(comd, chan, one, two);
				event = new MidiEvent(a, tick);
				
			} catch(Exception e) {e.printStackTrace(); }
			return event;
		}
		
	} // close class

Zgodovina sprememb…



Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[java] naloge za vajo

Oddelek: Programiranje
71389 (1075) Yacked2
»

[java] Osnovna vprašanja

Oddelek: Programiranje
372651 (1658) killa bee
»

[JAVA] branje iz datoteke

Oddelek: Programiranje
242424 (2059) Bela01
»

[Java]Vprašanja

Oddelek: Programiranje
242119 (1587) Bela01
»

[NALOGA][Java] Težave pri pisanju slikarja

Oddelek: Programiranje
202309 (1951) l0g1t3ch

Več podobnih tem