反垄断就是瞎胡闹

反垄断的目的是鼓励竞争,但竞争最终的目的就是减少竞争。反垄断最后得到的结果是没有鼓励竞争。竞争者的宿命就是永远都有潜在竞争者。—鲁迅

最近,阿里巴巴115亿罚单,滴滴下架,虎牙收购斗鱼被拒,谷歌脸书一次又一次反垄断起诉。全世界都加入了对大型科技公司反垄断的浪潮。港股科技股萎靡不振。反垄断法的目的是控制资本的无限扩大。当一个公司市场份额不断变大,反垄断机构开始担心,会不会对市场经济,自由竞争,消费者利益构成挑战。假设有多个竞争者,那么他们会因为竞争而压低产品价格,带给消费者利益。假设市场被一个公司垄断,那么这个公司就可以疯狂抬高价格,损害消费者利益,违背自由经济原则。反垄断法也成为了商业的宪法,任何有垄断倾向的并购,都会遭到反垄断部门的诉讼。

创立于1985年的美国DVD租碟公司Blockbuster红极一时,成为了美国最大的影响租借公司。2005年,Blockbuster想要收购当时市场份额排名第三的好莱坞影片租借公司。但美国反垄断部门阻止了这项收购,认为这个收购会增强Blockbuster的垄断地位,不利于竞争。但他们不知道的是网络直播公司奈飞NETFLIX的迅速崛起,给Blockbuster造成严重的威胁。反垄断部门干涉了Blockbuster的正常资金的投资和运营,干涉了自由市场。结局是这个所谓的垄断公司在未来几年果不其然江河日下,直到倒闭。反垄断部门那些所谓高瞻远瞩的执政者去决定什么能合并,什么不能合并,就正如你要皇帝去决定每个县包子的价格,最终造成的是破坏市场经济。

传统观点认为垄断企业会利用价格战打击弱小企业,等杀死这些企业后,垄断者会开始大幅度调高价格剥削消费者。然而,这在现实中是不会发生的。比如一个小镇上有个按摩店,因为价格公道,技艺娴熟,他打败了所有其他的按摩店,垄断了小镇的按摩业,让其他店全部倒闭。此时他把价格升了一倍。盈利疯狂上涨,老板睡着都能笑出声。但没过一周,一个做包子的叫乐乐耳子的男人看到了按摩行业可以如此赚钱,他立马联系了之前搞按摩的嘉琪霸霸,开了一家新的按摩店。价格只是垄断按摩店的一半。在新的按摩店占据市场1%的份额后,垄断按摩店觉得形势不对。为了杀死耳子和霸霸的店,把按摩价格降到比人工还低的亏本价格。3个月后,新按摩店倒闭。但垄断按摩店赔了3个月99%市场份额的利润,还要倒贴亏损的钱。把新店杀死后。他们接下来又调回了双倍的价格,想要弥补损失。这时,又有一家新店出现。。。。。这种以价格战消灭对手的方式最终是什么结局相信不难想到。

我们生活中实际看到的那些垄断企业非但没有抬升价格,反而拼命的降低价格。比如微信,他如此垄断,为什么不去收个月费?因为一个垄断企业想要保持他的垄断地位的唯一方法,是不断加强他和其他企业之间的护城河,通过加强管理与创新,给与消费者更物美价廉的商品。

19世纪末美国,洛克菲勒的标准石油公司在石油提炼成煤油过程中做了极大的创新。当时提炼的技术只能把60%的石油变成煤油。其他40%只能当废品倒入大海。而他却发明出了利用这40%废油的创新产品的方法,比如制作肥料,润滑剂,油漆,蜡烛,凡士林。这些副产品的销售极大降低了他的煤油生产总成本。以至于他可以给出其他所有竞争者无法匹敌的价格。1880年,他们占据了美国95%的市场,在如此垄断的情况下,在1880的前后10年间。他们的出售煤油却从1美金每加仑降了90%到10美分每加仑。这个所谓的垄断者保护了环境,降低了煤油价格,提高了人们生活水平,创造了无数的工作岗位。

你可能觉得也许是洛克菲勒是心地善良,所以降低价格,然而并不是。因为就像课本里讲的一样,资本从来是贪婪地。但洛克菲勒都垄断了为什么又要降低价格如此的讨好消费者呢。因为如果他不这么做,他就会失去他垄断的地位,永远都会有隐藏中的竞争者。当黑莓垄断手机市场的时候,搞电脑的苹果却突然杀入手机市场,夺取了半壁江上,后来安卓又一军突起,掠夺苹果份额。马化腾的QQ差点被雷军的米聊干掉,好在千钧一发之际祭出了微信。马云2014年员工大会上说,如果你们不努力,2013年将是阿里巴巴历史上最好的一年。这些所谓垄断企业对竞争者的恐惧要远远大于我们对于他们提升价格的恐惧。市场上不存在永远的垄断者,在风口浪尖的自由市场,逆水行舟,不创新不进步不去更讨好消费者,再垄断未来也就是死路一条。反垄断打破了自由经济的运作,垄断企业连定价都手足无措,价格调高,是以垄断姿态剥削消费者;降低价格,是打价格战;定个价格不高不低,不升不降,有人会去指责你这叫合谋性价格,也就是联手抬高价格。 综上。反垄断法实际是去强制保护被竞争淘汰下来的失败者。供应商提供廉价的服务本来是值得鼓励的,因为竞争使得消费者得益了,但是反垄断法的执行者,同时又去惩罚那些所谓的掠夺性定价行为或者说倾销的行为,而事实上,掠夺性定价或者倾销行为,是不能够使得厂商获得垄断地位的。日常生活中,我们见到的所谓掠夺性定价行为、倾销行为,往往不是出于垄断的意图,而是出于生产者效率的提高。说别人进行掠夺性定价、说别人倾销,往往是市场当中成本更高的生产者,指责更成功的竞争对手的一种借口。但反垄断法的执法者,往往又站到了这些在市场竞争当中失败的一方,组织他们被自由经济淘汰。

Advertisement

小岛经济学-1.经济是如何增长的

经济增长的原因

CS小岛上有两个渔民: 嘉琪霸霸、乐乐耳子。他们每天各只能抓一条鱼,刚好供他们各自一天的温饱。有一天嘉琪霸霸决定不苟且与此,他还想要诗和远方。他决定饿一天肚子,做一个渔网。渔网如果能做成,他每天可以捕两条鱼。这样他就可以工作一天,另一天去享受生活了。乐乐耳子摸着自己的胖肚肚笑道:“霸霸你肯定会失败的,到时我可不会管你”。霸霸不顾耳子嘲笑,竟然在第二天做出了渔网。从此霸霸一天可以补两条鱼,过上了工作一天休息一天的生活。小岛一天的总捕鱼量变成了三条,经济得到了增长。总结这个列子,CS小岛经济的长期增长靠的是霸霸的创新意识,创造出的渔网工具和他忍饥挨饿一天所得到的时间资本

借贷让经济加速发展

乐乐耳子看到霸霸一天能补两条鱼,哈喇子都滴到了地上,想要借渔网一用。但霸霸不愿意,因为渔网就只有一个,说:“你可以自己去做一个新的”。但乐乐耳子实在不想饿肚子,提议道:“不如你借我一条鱼,我到时换你两条”。霸霸同意了。乐乐用霸霸借给的一条鱼的资本做出了小岛第二个网。通过一条鱼的借贷,CS岛生产力从3条鱼变成了4条鱼。

万恶的资本主义….?

现实中,有很多仇富的人。“比尔盖茨,扎克伯格,凭什么你们好像不用干什么,却那么有钱。我拼死拼活,才这么一点钱。世界简直太不公平了!”。 “那些有钱人都自私的把钱存着自己享用,社会上那么多穷人他们却视而不见!”。其实真相是这样吗?

在霸霸拥有渔网和鱼,成为了一个小富人后,他可以干4件事。第一:每天把多出来的鱼自己吃掉,享受人生;第二:存起来。万一以后鱼少了到时可以应急。第三:把渔网借给乐乐耳子,要求乐乐耳子每天上交半条鱼给霸霸。第四,借给乐乐耳子一条鱼,让他自己去造渔网,但要求乐乐还两条鱼。

霸霸如何才能成为一个世界首富,拥有百万条鱼的渔翁呢?

一定不是把自己多出的鱼自己的存起来自己吃,这样鱼反而会越来越少。他必须把鱼或者网借出去给别人。帮助别人,也帮助自己捕鱼。然后用多的鱼再造新的网,再借给新的人。反复下去。第三第四件事乍看起来很剥削乐乐耳子。但其实,乐乐耳子的生活变的更好了,每天能吃得鱼更多了,肚子可以更大了。上面4件的哪一件事,其实都不影响他人,没有剥削任何人的财富,反而为别人创造了财富。

 想象生活中仇富的人,也许正因为有了他们,你才能过现在的生活。他的富有并不是来源于对你的剥夺,反而,你因为他的更有钱也变得更有钱了。

为什么需要投资实体黄金而不是ETF

很多人觉得黄金基金流动性好,方便买卖,也没有被盗的风险。管理费也不贵。所以都去买黄金基金。比如世界最大的黄金基金SLD,最大的白银基金SLV。或者国内支付宝里也可以购买博时黄金,华安黄金。这些基金对于短期投资很不错,ZLL借用杠杆炒纸黄金几天赚了10几万。但对于长期投资来说,其实里面存在着很大的风险。

黄金基金的运作方式

购买黄金基金不等于真的购买黄金。你买的等同于一个公司的股票。正常来说,买的人多了,股价上涨,买的人少,股价下跌。那黄金基金是如何做到跟踪黄金价格而忽略购买需求的呢?在你去购买了100股黄金基金后。他们会增发100股,去稀释掉你的买入带来的对股价的变化。所以股价不受你买入的影响。他们会用你这100股的钱去买一定数量的实体或期货黄金。过了几天,你发现黄金价格上涨,把这100股卖出。黄金基金会把他当时买入的实体黄金或者期货给卖掉。用这个卖掉的钱赎回你卖掉的那100股。那么这样,基金的股票价格就不会因为你把股票卖了而降低。而会始终跟踪着黄金价格变动。

黄金基金的危险

假设,黄金基金把所有投资人的钱都去买实体黄金。那这个模式就不存在任何问题。在经济危机来临后。他们只需要把实体黄金兑换成现金,就能保证他们的股价能准确跟踪黄金的价格。但是,黄金基金并不会把所有你买入他们股票的钱拿去买实体的黄金。他们成立的目的是为了赚钱。他们和银行一样。用的是杠杆模式的《部分准备金制度》。​你存钱进银行,银行留下10%的钱给你平时去取,剩下90%全部去借贷给别人赚钱。同样,黄金基金只用了小一部分去买黄金。​大部分的钱去用于别的商业用途。这个模式在经济正常的情况下没有问题。​但还记得2008年次贷危机有多少银行倒闭吗?

当下,各国政府疯狂借贷印钱,0利率使得整个金融体系过度的杠杆化,债务与收入的不匹配不断加大。灰犀牛其实就在不远处。当到了杠杆经济周期的最顶端。借贷,杠杆金融衍生品开始崩塌。流通的信贷会开始去杠杆化,整个经济通货紧缩。银行企业及各种经融机构开始倒闭。股票开始被大量抛售。黄金基金也不例外,他们手上只持有的少量的实体黄金,当现金出现短缺。他们没有足够黄金去兑换现金,去赎回你卖出的股票。造成他们的股价没有办法继续追踪黄金的价格。一个黄金基金持有的实体黄金越少,那么在特殊时期来临时,你所遭受的风险就越大。最后,当实体黄金突飞猛进开始往上涨后,也许你的黄金基金还在不停的往下跌。​

最后总结,不是你握在手上的黄金,那么不是你的黄金。

Invest safe.

记录生活

今天的柔术实战比上周六感觉体力好多了。。上周六的简直天旋地转。。拉了老王和JASON一起去,一定要想办法说服他们一直去。。

SIDE CONTROL ESCAPE还是不行,稍微会防了点,知道两手要CONNECT,还继续学YOUTUBE吧

 

 

睡成外星人的阿呆

%e7%85%a7%e7%89%87-16-9-8-%e4%b8%8b%e5%8d%887-41-30

REST constraints

• It must be a client-server system
• It has to be stateless—there should be no need for the service to keep users’
sessions; in other words, each request should be independent of others
• It has to support a caching system—the network infrastructure should
support cache at different levels
• It has to be uniformly accessible—each resource must have a unique address
and a valid point of access
• It has to be layered—it must support scalability
• It should provide code on demand—although this is an optional constraint,
applications can be extendable at runtime by allowing the downloading of
code on demand, for example, Java Applets

Coding styles – efficient code writing

The Single Responsibility Principle 
THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE

Click to access srp.pdf

one class should only have one responsibility. for example, in a server and client example. Client might have functions of make connection and sending messages. it sounds perfect reasonable but it actually has two responsibilities. The drawback of this is 1. eg when two different application use this class. one might only use the connecting function whereas the other one using chatting function. they have to include both codes in when compile. 2 when they change some code for connection method. it might result chatting function in class not working.

The Open-Closed Principle
http://www.objectmentor.com/resources/articles/ocp.pdf

A method should be deigned open to the extension but closed for the modification. The key is using abstraction. For example, you might have a method of drawShapes(ShapeType shapeType). in the method, you have case “SQUARE”, case “RECTANGLE” and so on. To add a CIRCLE in, you will have to edit the method body. Instead, you can have a interface of Shape, using add(Shape shape) to add in additional shapes. then iterate the list and draw the shapes. in this way, the principle is maintained.

Motherboard Port Guide: Solving Your Connector Mystery

If you’ve ever opened a PC case and stared inside, or looked at a bare motherboard, you may be taken aback by the number and variety of connectors, pins, and slots that exist on a modern PC motherboard. In this guide I’ll identify some of the most common (and a few uncommon) connectors on motherboards used in most home PCs. I won’t cover server- or workstation-class boards here, just what you might find in a typical midrange or high-end home PC.

For a similar discussion of the ports that you’re likely to encounter on the exterior of a PC case, see “Multiple Ports on Your PC: What Do They Do for You?

Since no single motherboard contains every type of connectors, I’ve used photos of four different boards to illustrate key examples. In one or two instances, there is some overlap; but for the most part, connectors are mentioned only once. Many of them may exist across different motherboard designs, however.

ADVERTISING

Asus P5WDH Deluxe

Let’s start with an older motherboard, an Asus P5WDH Deluxe. This motherboard has a few connectors that aren’t included on current-generation boards, as wll as some that do are still included, but are more readily visible here.

Connections on an older Asus P5WDH Deluxe motherboard.

Audio front panel: This ten-pin connector links to the front-panel headphone and microphone inputs. The particular connector shown is an AC97 connector, which existed prior to multichannel HD audio. It’s still in common use today.

Azalia digital audio header: You rarely find this connector, used to tie the motherboard to multichannel digital outputs on the case, on current motherboards.

Serial-port header: This connector isn’t physically present on the board shown–you can just see the solder points for it. But this header does appear on a few modern boards. It supports a nine-pin, RS-232 serial port, usually as a bracket that occupies a slot space on the back of the case. A number of RS-232 connections remain in use today, mostly in point-of-sale devices or specialized test instruments. Consumer boards typically don’t have them.

FireWire (IEEE 1994a): Once common as a digital camcorder interface, FireWire has largely been supplanted by USB, and the motherboard makers are gradually phasing it out. Some professional audio hardware still uses FireWire, though; you may also occasionally find higher-speed IEEE 1394b headers, but they are even rarer.

USB 2.0 front panel: These connectors are used to link to the front-panel USB ports on PC cases.

SATA connectors: These components connect via cables to various storage devices, including hard-disk drives, solid-state drives, and optical drives.

IDE connector: Rarely found today, IDE connectors were used to link to older hard drives. In addition, until a couple of years ago, many optical drives supported IDE. Today, all new storage devices ship with SATA.

Floppy disk connector: The venerable 3.5-inch floppy disk drive survived for nearly two decades–an eternity in the tech universe. But unless you have a pile of old floppies, you won’t need a floppy drive. And if you do find yourself needing a floppy drive, you can always pick up an external, USB-connected drive.

Intel DP67BG

Now let’s examine a more recent motherboard: an Intel D67BG, based on Intel’s P67 chipset and supporting LGA 1155 CPUs (like the Sandy Bridge-based Core i7-2600K).

Intel D67BG motherboard: a modern Intel design.

DDR3 memory sockets: Current-generation PC systems use DDR3 memory, but in many instances they support different operating speeds. The P67 chipset used in this board maxes out at DDR3-1600, but to achieve that level of speed you’d have to overclock the chipset–officially the P67 supports only DDR3-1333. Here, we see four memory sockets. The system supports dual-channel memory, meaning that the system is populated with paired memory modules, which are mounted in sockets of the same color.

CPU fan header: This connector is specifically designed to link to the CPU cooling fan. The system BIOS monitors CPU cooling fan speeds; and if the fan isn’t connected to this header, you may get an error at bootup.

Eight-pin ATX12V (CPU power) connector: Back when the Pentium 4 processor first shipped, Intel realized that high-performance CPUs needed their own source of clean, dedicated power beyond what the standard 24-pin power connector could deliver. Thus was born ATX12V. You’ll see four-pin connectors on lower-end boards supporting CPUs with lower thermal design power (TDP), but the eight-pin version of the connector is used with higher-end processors and on boards that users may overclock.

Power for secondary fans: Many motherboards with secondary-fan power headers; these connectors are mainly used to power and monitor various case fans.

PCI Express x1 connector: PCI Express is a serial interface, though multiple lanes may be ganged together. The “x1” refers to a slot supporting a single PCI Express lane; it is used for I/O devices that don’t require bidirectional bandwidth greater than 500 megabytes per second (gen 1 PCIe). Sound cards, for example, are typically PCIe x1 devices.

PCI Express x16 (graphics): PCI Express x16 slots are used mostly for graphics cards, though they can be used with any PCI Express card. Confusion may arise, however, because not all PCIe x16 slots are true PCIe x16. Occasionaly, you’ll see PCIe x16 connectors that are physical slots for accommodating graphics cards, but are actually eight-lane (x8) or even four-lane (x4) electrically.

On some boards, even slots that support true 16-lane PCI Express for graphics may revert to eight lanes if you install a second graphics card into a second PCIe x16 slot on the motherboard. The P67 chipset, for instance, has only 16 total PCIe lanes for graphics. So if you drop in two graphics cards to run in dual GPU mode, each card will have just eight lanes available to it. This situation isn’t as bad as it sounds, though, since even eight lanes in a PCIe 2.0- or 3.0-based system delivers plenty of bandwidth for most games.

32-bit legacy PCI slot: The now-classic 32-bit PCI slot has been around since 1993. A host of expansion cards support 32-bit PCI; and to accommodate them, most motherboards are likely to have at least one 32-bit PCI slot going forward. You may see some system boards configured so that a particular back-panel case bracket can support either a PCI slot or a PCIe slot, with some overlap between the two because they’re very close together.

Front-panel switch header: This header connects various wires to the front panel of the case, where they link to power and reset buttons, and status LEDs for power and storage-drive activity.

Gigabyte 990FXA-UD7

Next we’ll turn our attention to a motherboard that supports AMD CPUs. Note that AMD-chipset boards support many of the same features as Intel-based boards–that’s the great thing about industry standards.

An AMD-compatible motherboard: the Gigabyte 990FXA-UD7.

24-pin ATX power: This connector exists on all current ATX-based motherboards, and is the standard means of connecting power from power supplies. This connector delivers power to all interfaces, including 3V, 5V, and 12V. The typical ATX12V version 2.3 PSU delivers up to 75W for PCI Express graphics cards; but numerous modern graphics cards need more than that, which is why you’ll often find secondary six- or eight-pin power connectors on the graphics cards themselves.

ATX4P: This unusual item is actually a SATA power connector for delivering power to SSDs, hard drives, or optical drives from the motherboard itself.

TPM connector: Some off-the-shelf PCs and laptops use the Trusted Platform Module connector to link to a cryptographic processor module for storing encryption keys and handling dedicated encryption chores such as hard-drive encryption and certain types of digital rights management (DRM) decryption.

USB 3.0 front panel: This connector is used to drive front-panel USB 3.0 connectors. It requires more pins than USB 2.0 connectors do, but it drives two USB 3.0 ports instead of one. If your PC case lacks a USB 3.0 internal cable, you won’t be able to use it. Like back-panel connectors, front-panel USB 3.0 ports are often color-coded blue.

AMD CPU socket: I’m calling out this component because its style differs from that of a modern Intel CPU socket. AMD CPUs still have pins, whereas Intel has moved the pins to the motherboard socket.

Intel DZ77GA-70K

I’m using a photo of just one section of this board, to call out some specific connectors and to get a little closer in. The Intel DZ77GA-70K motherboard is designed to accommodate the latest Intel Z77 chipset.

Part of the Intel DZ77GA-70K–a motherboard that hosts the latest Intel Z77 chipset.

Case fan header: As noted earlier, most higher-end motherboards have several of these fan headers scattered around the board. If enough of them are available, you should connect your fans to them, so that the BIOS can monitor and manage the fan speeds–unless you’re a serious overclocker who uses separate fan-control modules.

PCI Express x4 slot: This relatively rare physical and electrical PCIe x4 slot is used for higher-performance networking cards and for some storage controller cards.

S/PDIF digital audio: This older type of connector was originally used to connect to CD-ROM drives. Today it’s still used to connect to some optical drives and other audio devices that support S/PDIF (Sony/Philips Digital Interface) digital audio.

USB 3.0 front panel: The DZ77GA motherboard ships with two front-panel USB 3.0 connectors, driving up to four USB 3.0 ports on the front of the PC case.

High-current USB 2.0 front panel: This is a slightly different type of USB 2.0 connector. Though it acts as a normal USB 2.0 port when sending or receiving data, it can deliver extra current to permit fast charging of mobile devices, and it can even charge devices (like Apple’s iPad) that requires more current than standard USB 2.0 normally delivers.

Consumer IR: This connector is used to attach front-panel infrared receivers, which enable users to control the PC via a standard programmable remote.

Diagnostic LEDs: Most motherboards have simple LEDs that light up or change color if the board experiences problems. A few higher-end boards, however, have these status LEDs, which flash an alphanumeric code that helps the user narrow down the source of a boot problem.

That wraps up our tour of various motherboard connectors, pins, and ports. Though I haven’t covered all of the possibilities by any means, the ones listed here account for the vast majority of connectors you’ll encounter on today’s motherboards.

Rarely will you use every single connector on a board, but understanding these connectors should help you choose a PC case that suits your needs, or that accommodates new devices that you plan to add in an upgrade. Also, if you have a specific need, you’ll be better able to shop for a motherboard capable of handling your application.

How to throw exception in java with example

In java we have already defined exception classes such asArithmeticException, ArrayIndexOutOfBoundsException,NullPointerException etc. There are certain conditions defined for these exceptions and on the occurrence of those conditions they are implicitly thrown by JVM(java virtual machine).

Do you know that a programmer can create a new exception and throw it explicitly? These exceptions are known as user-defined exceptions. In order to throw user defined exceptions, throw keyword is being used. In this tutorial, we will see how to create a new exception and throw it in a program using throw keyword.

You can also throw an already defined exception like ArithmeticException,IOException etc.

Syntax of throw statement

throw AnyThrowableInstance;

Example:

//A void method
public void sample()
{
   //Statements
   //if (somethingWrong) then
   IOException e = new IOException();
   throw e;
   //More Statements
 }

Note:

  • A call to the above mentioned sample method should be always placed in a try block as it is throwing a checked exceptionIOException. This is how it the call to above method should be done:
    MyClass obj =  new MyClass();
    try{
          obj.sample();
    }catch(IOException ioe)
     {
          //Your error Message here
          System.out.println(ioe);
      }
  • Exceptions in java are compulsorily of type Throwable. If you attempt to throw an object that is not throwable, the  compiler refuses to compile your program and it would show a compilation error.

Flow of execution while throwing an exception using throw keyword

Whenever a throw statement is encountered in a program the next statement doesn’t execute. Control immediately transferred to catch block to see if the thrown exception is handled there. If the exception is not handled there then next catch block is being checked for exception and so on. If none of thecatch block is handling the thrown exception then a system generated exception message is being populated on screen, same what we get for un-handled exceptions.
E.g.

class ThrowDemo{
   public static void main(String args[]){
      try{
	   char array[] = {'a','b','g','j'};
	   /*I'm displaying the value which does not
	    * exist so this should throw an exception
	    */
	   System.out.println(array[78]);
      }catch(ArithmeticException e){
	    System.out.println("Arithmetic Exception!!");
       }
   }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
78 at beginnersbook.com.ThrowDemo.main(Details.java:9)

Since the exception thrown was not handled in the catch blocks the system generated exception message got displayed for that particular exception.

Few examples of throw exception in Java

Example 1: How to throw your own exception explicitly using throw keyword

package beginnersbook.com;
class MyOwnException extends Exception {
   public MyOwnException(String msg){
      super(msg);
   }
}

class EmployeeTest {
   static void  employeeAge(int age) throws MyOwnException{
      if(age < 0)
         throw new MyOwnException("Age can't be less than zero");
      else
         System.out.println("Input is valid!!");
   }
   public static void main(String[] args) {
       try {
            employeeAge(-2);
       }
       catch (MyOwnException e) {
            e.printStackTrace();
       }
   }
}

Output:

beginnersbook.com.MyOwnException: Age can't be less than zero

Points to Note: Method call should be in try block as it is throwing an exception.

Example2: How to throw an already defined exception using throw keyword

package beginnersbook.com;
class Exception2{
   static int sum(int num1, int num2){
      if (num1 == 0)
         throw new ArithmeticException("First parameter is not valid");
      else
         System.out.println("Both parameters are correct!!");
      return num1+num2;
   }
   public static void main(String args[]){
      int res=sum(0,12);
      System.out.println(res);
      System.out.println("Continue Next statements");
   }
}

Output:

Exception in thread main java.lang.ArithmeticException: First parameter is not valid

Similarly other exceptions, such as NullPointerException,ArrayIndexOutOfBoundsException etc. can be thrown. That’s all for the topic how to throw exception in java. Let me know your feedback on this.

Throws clause in java – Exception handling

Use of throws keyword in Java

1. The throws keyword is used in method declaration, in order to explicitly specify the exceptions that a particular method might throw. When a method declaration has one or more exceptions defined using throws clause then the method-call must handle all the defined exceptions.
2. When defining a method you must include a throws clause to declare those exceptions that might be thrown but doesn’t get caught in the method.
3. If a method is using throws clause along with few exceptions then this implicitly tells other methods that – “ If you call me, you must handle these exceptions that I throw”.

Syntax of Throws in java:

void MethodName() throws ExceptionName{
    Statement1
    ...
    ...
}

E.g:

public void sample() throws IOException{
     //Statements
     //if (somethingWrong)
     IOException e = new IOException();
     throw e;
     //More Statements
}

Note: In case a method throws more than one exception, all of them should be listed in throws clause. PFB the example to understand the same.

public void sample() throws IOException, SQLException
{
    //Statements
}

The above method has both IOException and SQLException listed in throws clause. There can be any number of exceptions defined using throws clause.

Complete Example of Java throws Clause

class Demo
{
   static void throwMethod() throws NullPointerException
   {
       System.out.println ("Inside throwMethod");
       throw new NullPointerException ("Demo"); 
   } 
   public static void main(String args[])
   {
       try
       {
          throwMethod();
       }
       catch (NullPointerException exp)
       {
          System.out.println ("The exception get caught" +exp);
       }
    }
}

The output of the above program is:

Inside throwMethod
The exception get caught java.lang.IllegalAccessException: Demo