How to create an exit message
The abort
function does this. For example:
abort("Message goes here")
Note: the abort
message will be written to STDERR
as opposed to puts
which will write to STDOUT
.
How to throw error and exit with a custom message in python
Calling sys.exit
with a string will work. The docs mention this use explicitly:
In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
How to exit a programm with an error message and error code in bash?
You can use Use { ...; }
instead of (...)
to avoid the sub-shell and the problem that causes and to get the result that you want.
Note that a space after the opening {
and a semicolon/etc. and a space before the closing }
are required for this (where they are optional with ()
).
As to why ls || echo && exit
doesn't do what you expect the answer is because the ||
and &&
operators have the same precedence and are left-associative (see the POSIX spec).
So when the shell sees
ls || echo "Having trouble" && exit 1;
you think it is
ls || { echo "Having trouble" && exit 1; }
but the shell parses it as
{ ls || echo "Having trouble"; } && exit 1;
Or, as the spec puts it:
The operators "&&" and "||" shall have equal precedence and shall be evaluated with left associativity. For example, both of the following commands write solely bar to standard output:
false && echo foo || echo bar
true || echo foo && echo bar
Message Box Dialog at Exit using Tkinter in Python
You can use the protocol
method to bind the window deletion with a function.
from tkinter import *
from tkinter import messagebox
def on_close():
response=messagebox.askyesno('Exit','Are you sure you want to exit?')
if response:
root.destroy()
root=Tk()
root.protocol('WM_DELETE_WINDOW',on_close)
root.mainloop()
UPDATE
According to the docs of atexit
module
Functions thus registered are automatically executed upon normal interpreter termination.
The function registered was called after the mainloop
was destroyed (since nothing proceeds, it marks the end of program). The GUI element that the function tries to destroy doesn't exist anymore, as also stated by the error.
This module is not meant for the use case you trying to achieve, it's usually used for "cleanup" functions that are supposed to perform a task after the program terminates.
The callback registered via the WM_DELETE_WINDOW
protocol gives you the control over what happens when the window is instructed to close.
How to programmatically exit from a second message loop?
You should pass the Form
instance to the ApplicationContext
ctor as a parameter:
applicationContext = new ApplicationContext(form);
Right now, you are basically creating a no-context instance, which doesn't care about your form being closed.
Also, it is a good practice to do some cleanup, like removing the filter when you don't need it anymore:
Form form = new Form();
ApplicationContext applicationContext = new ApplicationContext(form);
handleHelper(form.Handle);
MessageFilter filter = new MessageFilter(filter);
Application.AddMessageFilter(filter);
Application.Run(applicationContext);
Application.RemoveMessageFilter(filter);
[Edit]
If you don't want to show the form, then you can use the paramaterless ctor, but you will have to close the context manually by calling the ApplicationContext.ExitThread method. This method actually gets called when your form fires the FormClosed
event, if you pass the form in the constructor.
Since hidden form is not related to the context, you need to exit them both at some time.
how to show the popup message on Exit button
JavaScript:
<input type="button" value="Exit" onclick="confirmBox()" />
function confirmBox(){
if (confirm('Are you sure you want to Exit?')){
window.location.href = '../index'
}
}
jQuery:
<input type="button" value="Exit" id="exit" />
$('#exit').click(function(){
if (confirm('Are you sure you want to Exit?')){
window.location.href = '../index'
}
});
How do I show a message before system.exit in Java
I would use the YES_NO_CANCEL_OPTION:
Object[] options = {"Yes","No","Cancel"};
int n = JOptionPane.showOptionDialog(frame,
"Continue?",
"Would you like to continue?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if (n == JOptionPane.YES_OPTION) {
System.out.println("Clicked Yes");
} else if (n == JOptionPane.NO_OPTION) {
System.out.println("Clicked No");
} else if (n == JOptionPane.CANCEL_OPTION) {
System.out.println("Clicked Cancel");
} else {
System.out.println("something else (like clicked the 'x' button)");
}
exit with error message in bash (oneline)
exit
doesn't take more than one argument. To print any message like you want, you can use echo
and then exit.
[[ $TRESHOLD =~ ^[0-9]+$ ]] || \
{ echo "Threshold must be an integer value!"; exit $ERRCODE; }
Related Topics
Storing Arrays in Database Using Activerecord
Use of Caret Symbol (^) in Ruby
Missing Host to Link To! Please Provide the :Host Parameter, for Rails 4
How to Write Rake Task to Import Data to Rails App
How to Remove a Node with Nokogiri
Doing a Http Basic Authentication in Rails
Rails - Pass Id Parameter on a Link_To
Failing Installing Pg Gem, "Mkmf.Rb Can't Find Header Files for Ruby" (MAC Osx 10.6.5)
Check If Internet Connection Exists with Ruby
Loaderror Running Mongrel with Rails3 and Ruby 1.9.2
How to Get Indexes of All Occurrences of a Pattern in a String
Why Doesn't Ruby Have a Real Stringbuffer or Stringio
Rails - Rspec - Difference Between "Let" and "Let!"
Insecure World Writable Dir /Users/Username in Path, Mode 040777 When Running Ruby Commands